Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Digital Watch program and Sorting arrays

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Digital Watch program and Sorting arrays

    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
    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
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
            at Sorting.main(Sorting.java:6)


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Digital Watch program and Sorting arrays

    its an run time exception.thats why it compiles fine.

    if(a[i]>a[i+1]){
    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
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Sorting.main(Sorting.java:6)
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. The Following User Says Thank You to DanBrown For This Useful Post:

    c.P.u1 (February 4th, 2011)

  4. #3
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Digital Watch program and Sorting arrays

    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

    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.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  5. The Following User Says Thank You to DanBrown For This Useful Post:

    c.P.u1 (February 4th, 2011)

  6. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Digital Watch program and Sorting arrays

    Quote Originally Posted by DanBrown View Post
    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?
    Last edited by c.P.u1; February 4th, 2011 at 10:17 AM.

  7. #5
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: Digital Watch program and Sorting arrays

    i have read that we need to use new lines for the output to get on the first line again... any idea about that?

    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.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  8. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Digital Watch program and Sorting arrays

    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
    String[] cmd = {
        "cmd.exe",
        "cls"
    };
    Runtime.getRuntime().exec(cmd);
    Last edited by Brt93yoda; February 5th, 2011 at 02:30 AM.

  9. #7
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Red face Re: Digital Watch program and Sorting arrays

    I had tried both , but not working.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  10. #8
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Digital Watch program and Sorting arrays

    In linux I just do Runtime.getRuntime().exec("clear");

  11. #9
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Digital Watch program and Sorting arrays

    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.
    Last edited by DanBrown; February 7th, 2011 at 03:12 AM.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  12. #10
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Digital Watch program and Sorting arrays

    I'm sorry, but I can't understand your broken English.

Similar Threads

  1. [SOLVED] Using Arrays to make a scoring program
    By woodcutterni in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 7th, 2011, 07:02 PM
  2. Sorting/Lexicographic =)
    By jcs990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 12th, 2010, 11:19 PM
  3. thread sorting
    By thanos_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 06:23 PM
  4. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM