why does my while statement not work ?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: why does my while statement not work ?

  1. #1
    Join Date
    Nov 2003
    Posts
    29

    why does my while statement not work ?

    The snippet below is one constructor i have for the class TimeFun. The value s is the current (get) seconds value.

    Code:
    TimeFun(int s)
    {
    
    int timestart = s;
    Calendar Tim = new GregorianCalendar();
       
    do {
    System.out.print("A second");
    }
    while (Tim.get(Calendar.SECOND) == timestart);
    
    System.out.print("A second has expired");
    
    }
    i have been experienting with this code, ideally im trying to make a timer count up from 0, but without using a thread. I simply cannot understand why the while always evaluates to true.

    Im not looking for an alternative way to solve the problem, just an explanation of why the output is an infinite stream of System.out.print

    Thanks

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Because that's not how Calendar works. Did you check the Calendar API?

    http://java.sun.com/j2se/1.4.2/docs/.../Calendar.html

    The object you're creating doesn't automatically update with the current time.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center