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 5 of 5

Thread: Printing out numbers from an Array.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Printing out numbers from an Array.

    Hi i have the following little programme which prints out some numbers from an Array i declared at the start which holds 5 values. Once the 5 values are printed the programme catches the exception and prints out the message, as there are no more numbers to print!.

    Here is the code.

    package ClientServer;
     
    public class GoTooFar {
     
        public static void main(String[] args) {
     
            int dan[] = {26, 42, 55, 67, 43};
     
            try {
     
                for (int counter = 0; counter <= dan.length; counter++) {
     
                    System.out.println(dan[counter]);
                }
     
            } catch (ArrayIndexOutOfBoundsException e) {
     
                System.out.println("Youve gone too far");
     
            }
        }
    }


    As you can see the numbers seem to printed out all at the same time, (although scientifically i guess there is a fraction of a second between each one).

    My question is how can i make there be 1 second in between printing out each digit?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Printing out numbers from an Array.

    Use Thread.Sleep(1000). Incidentally, you'll need to catch the InterruptedException which could be thrown by this method.

    It's a bit worrying that you're using the exception to terminate the loop, though... I'd get rid of it and change your conditional to use a less than rather than less than or equal to (unless there's some other reason such as your class requires it).

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing out numbers from an Array.

    Hi,

    I have done as you advised with removing the exception handler and just changed the condition to less than.

    Regarding the Thread and sleep part, can i do this in the same class or is a new one needed.?

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing out numbers from an Array.

    You can do it in the same class.

    and as for the InterruptedException, you can just throw it when you start your "main" method.

    Thread.sleep(# of milliseconds); (just in case you want to change the time in between)

    For example:

    public static void main(String[] args) throws InterruptedException
    {
        for (int i = 0; i < 5; i++)
        {
            System.out.println(i);
            Thread.sleep(1000); //sleeps for 1 second
        }
    }

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing out numbers from an Array.

    Many thanks, all working

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  2. Printing an array
    By native in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 16th, 2011, 09:07 AM
  3. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  4. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM