How to Do Press Any Key to Continue in Java
How do I get "Press any key to continue" to work in my Java code?
Solution 1
You can create this function (good only for enter key) and use it where ever you want in your code:
private void pressAnyKeyToContinue() { System.out.println("Press Enter key to continue..."); try { System.in.read(); } catch(Exception e) {} }
Solution 2
1) See while(run1 = true)
and while(run2 = true)
= is assignment operator in java. use == operator to compare primitives
2) You can do like this
while(in.hasNext()){ }
Solution 3
Before getting into implementation details, I think you need to step back and re-examine your algorithm a bit. From what I gather, you want get a list of integers from the user and determine if they form a magic square. You can do the first step in a single while loop. Something like this pseudo-code:
while (true) print "Enter an integer (x to stop): " input = text from stdin if input is 'x' break else if input is not an integer print "non integer value entered, aborting..." return else add input to magic object
After that, you can output details about the numbers:
if magic is a magic square print "this is a magic square" else print "this is not a magic square" // etc, etc.....
Solution 4
My answer still only works with the Enter key but it does so without leaving stuff on the input stream that could get in the way later (System.in.read() can potentially leave things on the stream that get read in on the next input). So I read the whole line (here I use Scanner but I guess that isn't actually necessary, you just need something to get rid of the entire line in the input stream):
public void pressEnterKeyToContinue() { System.out.println("Press Enter key to continue..."); Scanner s = new Scanner(System.in); s.nextLine(); }
Related videos on Youtube
Comments
-
I want the user to enter information again in the first while loop after pressing any key on the keyboard. How do I achieve that? Am I doing something wrong with the while loops. SHould I just have one while loop?
import java.util.Scanner; public class TestMagicSquare { public static void main(String[] args) { boolean run1 = true; boolean run2 = true; Square magic = new Square(); Scanner in = new Scanner(System.in); while(run1 = true) { System.out.print("Enter an integer(x to exit): "); if(!in.hasNextInt()) { if(in.next().equals("x")) { break; } else { System.out.println("*** Invalid data entry ***"); } } else { magic.add(in.nextInt()); } } while(run2 = true) { System.out.println(); if(!magic.isSquare()) { System.out.println("Step 1. Numbers do not make a square"); break; } else { System.out.println("Step 1. Numbers make a square"); } System.out.println(); if(!magic.isUnique()) { System.out.println("Step 2. Numbers are not unique"); break; } else { System.out.println("Step 2. Numbers are unique"); } System.out.println(); magic.create2DArray(); if(!magic.isMagic()) { System.out.println("Step 3. But it is NOT a magic square!"); break; } else { System.out.println("Step 3. Yes, it is a MAGIC SQUARE!"); } System.out.println(); System.out.print("Press any key to continue...");// Here I want the simulation in.next(); if(in.next().equals("x")) { break; } else { run1 = true; } } } }
-
You can't. Basically Java's implementation of the stdin will only return when the use presses [Enter]. You could use a JNA or JNI solution, but that might be more work then is worth it...
-
Add an action listener to the keyboard.
-
-
This doesn't work for "ANY" key, just the "Enter" key.
-
As @katamayros mentioned, it works with enter and not with "any key"
-
@katamayros and Hammad Dar, You are right. I updated my answer.
Recents
Related
Source: https://9to5answer.com/how-do-i-get-quot-press-any-key-to-continue-quot-to-work-in-my-java-code
0 Response to "How to Do Press Any Key to Continue in Java"
Post a Comment