|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Press any key to continue
Hello all. I'm am pretty new to java programming. I am currently working on a program in class right now. What I wanted to know was, how do I do the code that says "Press any key to continue . . ." and it end the program after someone presses the any key ha. It is not required, but if I learn how to do it, I can incorporate it into my future programs in other ways as well. Thanks for the help.
|
|
#2
|
||||
|
||||
|
Do your programs have any keyboard input?
The hard way would be to use InputStreams and ReadBuffers and all sorts of stuff you probably haven't learnt yet. A better idea might be to ask your instructor. =) I remember back in college we used a Java Class (provided by the text book) that took care of keyboard input. We had to then do something like: KeyboardInput kbd = new KeyboardInput(); System.out.println("Press ENTER key to continue..."); kbd.getChar(); note, this won't work for you as your Java compiler won't know what KeyboardInput is The basic logic is, display the prompt and accept user input. Our problem was "any key" wouldn't work, it had to be enter. Again, I urge you to ask your instructor =) I might be able to dig up those simple Class files that came with my textbook if you really want it. |
|
#3
|
|||
|
|||
|
We are given the MyInput class where it takes what the user types and I can store it. Essentially, I could make the program say "Press ENTER to continue..." but I just wanted to do like other programs that do the any key. I'll my ask my instructor tonight after class probably. Thanks for the help
|
|
#4
|
||||
|
||||
|
Our solution back in college, with the java classes provided, only responded to ENTER.
If you do find the answer, be sure to post it here. [although it might not do any good without your specific input classes] |
|
#5
|
|||
|
|||
|
If using scanners. try this out.
Prior to your system.out.println("Press enter to continue"); you have used a line of code that says nextInt() or nextLine() or something to that effect. To fix this problem use the following code: Scanner keyIn = new Scanner(System.in); System.out.print("Press the enter key to continue"); keyIn.nextLine(); keyIn.nextLine(); the first keyIn.nextLine will end the nextInt() or nextLine that was used before the System.out line and the 2nd will tell your code to continue onto the next portion when you hit enter. Hope this was clear enough |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Press any key to continue |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|