user1959349
user1959349

Reputation:

java.util.scanner Explanation

Dear Secretive Hackers and Legendary Programmers,

I already know that the scanner is used to recognize user input much like the java bufferedreader in the io section of the virtual java handbook. The question is how can you apply the scanner in the simplest terms, meaning beginning with a set button such as {A} or say {1} for instance? How do I apply the scanner to get java to realize these buttons were pressed, and if so, then print the set condition?

Logic: Scanner application??... (I need help with this)

If A, then System.out.println("Hi, my name is A!!")

If 1, then System.out.println("Hi, my name is 1!!")

Thanks much

Upvotes: 0

Views: 272

Answers (1)

FlutterDashie
FlutterDashie

Reputation: 108

To the extent of my (somewhat limited) knowledge, a Scanner reading System.in will not pass any data until the enter (return) key is pressed (as until that point, it has no data to pass), so it would not respond to lone button presses. It will respond if you do press enter, so use something like:

Scanner yourScanner = new Scanner(System.in);
//Creates a scanner that reads from the terminal.
System.out.println("What is my name? "); 
//Whatever you want the user to be asked, let them know what to input.
String theirResponse = scan.next(); 
//Or .nextLine() if the input contains a space in the middle
System.out.println("Hi, my name is " + theirResponse + "!!");
//Prints out and uses their response.

Unfortunately, Scanner is not a very good way to go for key listening.

Upvotes: 2

Related Questions