Monday, July 27, 2009

How can i set a time limit for user input in a java program?

i m taking some input from the user in a java program. and i wanna set a time limit within which the user should provide this input. if no input is provided during this time interval the program goes to the immediate next line and sets the user input to a space (ASCII code 32). other wise the user input is set to whatever is entered by the user.


im in high skool so preferably solution shouldnt be really really complex. and it should be appropriate.


Any help will b appreciated.

How can i set a time limit for user input in a java program?
The problem is you are doing two things ... you are taking input and you are running a stopwatch that is looking for any input. This is trivial with a Swing GUI. I would have to research if this is an interactive terminal program.





============


Ok, I did find a "timer" running in a Terminal. They take a Calendar to get the current milliseconds, count 1000 milliseconds, then write to System.out.println.





I guess, you would have to have a while loop, System.out.print("With " + tTime + " to go... Do you have any answer? [ y / n ] ");





I tried to make it work by using ^H (char backspace). You know, loop it so it would say...


Time left is 32 ... pause System.print([back][back])


System.print(aTime);





But, that doesn't work.





And, hey, java uses UTF-16 unicode. So...


char space = new Character('\u0032');


or you could use a String...


String space = " ";


or


String space = "\\u0032";
Reply:You can refer http://java.sun.com and click help or some support button to get help.


There is one more feature which I think you don't know.


Go to JDK folder and then click "Examples" folder. There you can find many Applet programs in which one of them may contain timer.


No comments:

Post a Comment