Results 1 to 5 of 5

Thread: Using a Timer

  1. #1
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default Using a Timer

    I am fairly new to Java and I'm just trying to figure out how to use a timer to do something in a certain amount of time. What I'm trying to do, specifically, is make it so that each letter of a string appears after a certain amount of time. Here's my code:
    Java Code:
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class Main
    {	
    	public static void main(String[] args)
    	{
    		String helloString = "Hello Everybody!";
    		Timer time = new Timer(1000, this);
    		int counter;
    		
    		for(counter = 0; counter == helloString.length(); counter++ )
    		{
    			System.out.println(helloString.charAt(counter));
    		}
    	}
    }
    As you can see I have a Timer object created(I'm not even sure if I even used the correct parameters) but it's not implemented because I don't know how to use it. Any help would be appreciated.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    What does your compiler say about your code?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    What does your compiler say about your code?

    kind regards,

    Jos
    It's not an error that I'm trying to deal with. I'm just trying to figure out how to use the java timer class.

  4. #4
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Please somebody help.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by iamsothecoolest View Post
    It's not an error that I'm trying to deal with. I'm just trying to figure out how to use the java timer class.
    We are conditioned towards seeing and trying to fix errors if and when we find them, and it was probably obvious to Jos that your code posted does not compile which is likely why he made his recommendation.

    Anyway, to answer your possible question, there are at least three Timer classes in the core Java library, two of which you'll commonly use, the javax.swing.Timer for Swing gui applications that need a Timer functionality and the java.util.Timer for non-Swing timer needs. So, you'll first need to ask yourself for what purpose you'll be using the timer eventually, and then next check out the Java API for the Timer class.

    Quote Originally Posted by iamsothecoolest View Post
    Please somebody help.
    Also, we're all volunteers here helping when we have a chance. Please don't bump your post < 12 hours after posting, lest you want to appear impatient.

Similar Threads

  1. Timer And Timer Task
    By Ashish_jain in forum New To Java
    Replies: 3
    Last Post: 06-22-2011, 09:41 AM
  2. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  3. Timer
    By Learning Java in forum New To Java
    Replies: 3
    Last Post: 08-11-2010, 12:18 PM
  4. Timer
    By barts2108 in forum New To Java
    Replies: 4
    Last Post: 06-08-2010, 06:17 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •