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

Thread: My while (true) loop method only seems to run once!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My while (true) loop method only seems to run once!

    Hi guys,

    I've recently written a block of code to serve as an internal integer-based clock for another program I'm using.

    public class WorldTime
    {
        public String gametime;
        final int sunrise = 600;
        final int sunset = 1800;
        public int timevalue = 0;
        public Boolean isnight;
        public Boolean isday;
        public Boolean issunrise;
        public Boolean issunset;
        public Boolean runclock;
     
        public int currentTime()
        {
            while (true)
            {
     
                try 
                {
                    Thread.sleep(1000);
                }
     
                catch (InterruptedException timevalue)
                {
     
                }
     
                if (timevalue == 2400)
                {
                    resetTime();
                }
     
                else
                {
                    timevalue++;
                }
     
               return timevalue;
            }
        }

    There are a lot more other methods in the class, but this is the only one I'm interested in for this question.

    Now, to test that this counting system is working, I created a dummy program to merely test to method value to see if this method was working.

    public class TimeTest 
    {
        public static int cool;
     
        public static void main(String args[])
        {
            WorldTime base = new WorldTime();
            cool = base.currentTime();
            testRun();
        }
     
        public static void testRun()
        {
            Scanner scan = new Scanner(System.in);
            int x = scan.nextInt();
     
            int y = new WorldTime().currentTime();
     
            if (x > 0)
            {
                try 
                {
                    Thread.sleep(5000);
                }
     
                catch (InterruptedException timevalue)
                {
     
                }
     
                System.out.println("The program's time is: " + cool);
            }
        }
    }

    The code compiles easily.

    I have it sleep for 5 seconds to see if the counting method works. However, the output is always "1", regardless of how long the sleep value is, or how long I wait to input an x value.

    What's going on here? I'm not exactly a java expert, but I have enough experience writing code that I thought this should work.

    Thanks in advance!


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: My while (true) loop method only seems to run once!

    Quote Originally Posted by ajfonty View Post

            while (true)
            {
     
    ...
     
               return timevalue;
            }
        }
    What do you think the return does? Follow the code you have through - it looks pretty straightforward that it will only execute once.
    Need Java help? Check out the HotJoe Java Help forums!

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: My while (true) loop method only seems to run once!

    You return a value in your loop. Whenever you return something, all of the execution afterwards is ignored.

Similar Threads

  1. boolean method not checking to see if within given tolerance to be true.
    By lemonlimesoap in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 3rd, 2012, 01:28 PM
  2. Help bolean method and while loop !!!!
    By pip0 in forum Loops & Control Statements
    Replies: 15
    Last Post: April 2nd, 2012, 08:29 PM
  3. [SOLVED] isPrime Boolean Method returns true for squares of primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 29th, 2011, 11:46 PM
  4. Threads are killed randomly in While true loop
    By kurt-hardy in forum Threads
    Replies: 7
    Last Post: August 11th, 2011, 07:55 AM
  5. POST method in a loop
    By kollyisrealisaac in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 26th, 2011, 11:29 AM