InputStream in read user input from terminal bytes by bytes. We can use the Scanner class to read the whole string.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your username: ");
String username = in.nextLine();
System.out.println("Username : " + username);
}
To read integer, try enter any single digit from 0 – 9,
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter extension line 0 - 9: ");
int line = in.nextInt();
System.out.printf("You've select line %d \n", line);
}
What if you allow to enter more digit separated by space?
Scanner in = new Scanner(System.in);
System.out.print("Enter extension line 0 - 9: ");
while (in.hasNextInt()) {
System.out.printf("You've select line %d \n", in.nextInt());
}
}
Java – Scanner