An easy way for Java to accept input characters from the console

  • 2020-04-01 02:53:41
  • OfStack

Create a class that wraps the in input stream of the System class in a Scanner scan from the main method of the class, then prompt the user for an id number and enter the number of digits of the id number.

The code is as follows:


import java.util.Scanner;
public class InputCode {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//Create an input stream scanner
        System.out.println(" Please enter your id number: ");//Prompt the user for input
        String line = scanner.nextLine();//Gets a line of text entered by the user
        //Print a description of the input text
        System.out.println(" So your id number is " + line.length() + " It's in digits ");
    }
}


Related articles: