Console Input

To read a String from console input in Java use the class Scanner. This class provides several methods for reading strings or numbers. Before you can read input with the Scanner an instance has to be created:

Scanner scanner = new Scanner(;

In this case "in" is a standard input stream provided by the System class which is used by the Scanner class to read the input of the user. In order to read a string use the method nextLine:

String inputString = scanner.nextLine();

To read a value of a certain data type use dedicated methods for these data types. For example to read an integer value use the method nextInt. The Scanner will wait for the next integer the user will input to the console:

int inputInteger = scanner.nextInt();

results matching ""

    No results matching ""