Scanner object can be used to get inputs from keyboard in java. It is analogous to cin or scanf in C/C++. The Source code is given below. Here it will come out from the loop when you press 0. the code will wait in scanner.hasnext() function for an entry and "Enter" key.
Scantut.java
This is useful when we create a command line based interface for our java project.
Scantut.java
import java.util.Scanner;
public class Scantut {
public static void main ( String[] args ) {
Scanner scanner = new Scanner(System.in);
int selection = 2000;
while ( scanner.hasNext() ) {
try {
selection = Integer.parseInt(scanner.next());
} catch( NumberFormatException e ) {
System.out.println("Invalid Selection");
}
if ( selection == 0 ) {
System.out.println("Closing the server");
break;
}
}
}
}
This is useful when we create a command line based interface for our java project.