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: Count and Print 1 to 10 in Real Time - Help :S

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

    Default Count and Print 1 to 10 in Real Time - Help :S

    Hi all,

    I need to make a Program that once the user clicks run, the Program prints out 1 to 10 with a second gap in between so it actually prints out in real time:

    The output would just be:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10



    But would print 1, then wait a second then print 2, then wait a second then print 3, etc. etc. until 10. Then the Program would just finish as normal.


    How easy is would this be ? Im not asking for code but if someone could point me in the right direction as to what classes or methods i need to be using, and any advice you feel necessary, then that would be fantastic. Bearing in mind i am a beginner.


    P.S. If i havn't made the requirements entirely clear then please don't hesitate to ask a question.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    That should be easy. Look at the Thread class's sleep() method to handle the delay inside of a loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    Sounds like a simple call to Thread.sleep() would do the trick.

    Alternatively you could use a Timer.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    It doesnt seem to like this:

       public static void main(String[] args) {
     
            for (int counter = 1; counter <= 10; counter++){
                System.out.println(counter);
                Thread.sleep(1000);
            }
        }
    }

    Thread.sleep(1000); is red underlined and says:

    Exception in thread "main" java.lang.RuntimeException:

    Do i need to throw an exception ?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    Can you post the full text of the error message that shows where the exception happened?

    You don't need to throw an exception.

    The code you posted should generate a compile time error, not a runtime error. Just compile it, don't try to execute it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    Here is the full error:

    run:
    1
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
    at latesttasks.task15.main(task15.java:14)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    regards.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
    This part says that the sleep() method can throw an exception. Your code must be ready to handle that exception. The normal way is to put that statement inside of a try{}catch(){} block to "catch" the exception.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    Having added the try catch block i have the following:

    package latesttasks;
     
    // Count and Print 1 to 10 in Real Time. 
    // Threads
    import java.util.*;
     
    public class task15 {
     
        public static void main(String[] args) {
            try {
                for (int counter = 1; counter <= 10; counter++) {
                    System.out.println(counter);
                    Thread.sleep(1000);
                }
             catch  (InterruptedException e){
             }
        }
    }


    But the catch line is red underlined and the program now runs and prints the 10 numbers but then the error occurs.

    here is the output:

    run:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 'catch' without 'try'
    at latesttasks.task15.main(task15.java:15)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 11 seconds)

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    Check that the {}s are properly paired.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Count and Print 1 to 10 in Real Time - Help :S

    They appear to be correctly paired

    I should also add 'sleep' is yellow underlined and it says 'Thread.sleep called in loop'

    --- Update ---

    Update:

    All fixed. Thanks for your help.

Similar Threads

  1. Superaxander(real name Alexander)
    By Superaxander in forum Member Introductions
    Replies: 3
    Last Post: November 2nd, 2012, 12:28 PM
  2. First real program help
    By CoolYoungHipKiddo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 14th, 2012, 04:43 PM
  3. Testing if dates are real or not.
    By cardsfan33 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2011, 07:46 PM
  4. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 09:10 AM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM