Digital Watch program and Sorting arrays
Code java:
class time{
public static void main(String args[])
{
for(int h=0;h<=24;h++)
{
for(int m=0;m<=60;m++)
{
for(int s=0;s<=60;s++)
{
try
{
Thread.sleep(1000);
}catch (InterruptedException ie)
{
System.out.println(ie.getMessage());
}
System.out.println(h+":"+m+":"+s);
}
}
}
}
}
The program is correct but i want the screen to be cleared after every time it displays... i searched a lot for a statement which would clear the screen but i dint find any relevant result... i use command prompt for compiling and running java programs...
The second program is
Code java:
class Sorting{
public static void main(String args[]){
int a[]={5,4,9,3,6,2};
int b=a.length;
for(int i=0;i<b;i++){
if(a[i]>a[i+1]){
int temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
for(int i=0;i<b;i++)
System.out.println(a[i]+"\t");
}
}
it compiles well but while running the class file the compiler issues the following warning
Code :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Sorting.main(Sorting.java:6)
Re: Digital Watch program and Sorting arrays
its an run time exception.thats why it compiles fine.
my dear what will happen in this case
when i=5
then a[i+1] will refer to position a[6], which is not available in your array.
so it will give you
Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Sorting.main(Sorting.java:6)
Re: Digital Watch program and Sorting arrays
Quote:
the screen to be cleared after every time it displays... i
when i read this question , my first attempt is to try to print the new time at (x,y) position, i tried this but no result.Then i fell we can execute dos command using
Quote:
String cmd[] = new String[3];
cmd[0] = "command.com" ;
cmd[1] = "/c";
cmd[2] = "cls";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
i am again fail, i google it and find that the above code will work for win9x only.
:-w
Re: Digital Watch program and Sorting arrays
Quote:
Originally Posted by
DanBrown
its an run time exception.thats why it compiles fine.
my dear what will happen in this case
when i=5
then a[i+1] will refer to position a[6], which is not available in your array.
so it will give you
thanks bro.. i corrected that part... :)
and for the second program, i have read that we need to use new lines for the output to get on the first line again... any idea about that? :-/
Re: Digital Watch program and Sorting arrays
Quote:
i have read that we need to use new lines for the output to get on the first line again... any idea about that?
Code :
for(int i=0;i<25 ;i++)
{
System.out.println("");
}
when i m searching for this , some programmers are also working like this.
our command prompt is divided into 25 rows and 80 columns
so they are moving to 25 lines so that start again from the first line.
Hope you are asking for this technique.
Re: Digital Watch program and Sorting arrays
Quote:
String cmd[] = new String[3];
cmd[0] = "command.com" ;
cmd[1] = "/c";
cmd[2] = "cls";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
I think you have to use cmd.exe not command.com.
try this
Code java:
String[] cmd = {
"cmd.exe",
"cls"
};
Runtime.getRuntime().exec(cmd);
Re: Digital Watch program and Sorting arrays
I had tried both , but not working.
Re: Digital Watch program and Sorting arrays
In linux I just do Runtime.getRuntime().exec("clear");
Re: Digital Watch program and Sorting arrays
Quote:
String[] cmd = {
"cmd.exe",
"cls"
};
Runtime.getRuntime().exec(cmd);
In win 9x and above this will work fine and i don't know the reason why its not running for winxp and above.
Re: Digital Watch program and Sorting arrays
I'm sorry, but I can't understand your broken English.