Scanner input problem

1. If all integers involved by the keyboard are received: [enter the keyboard to enter the next line, it is normal and normal, and there are no accidents such as error reporting]

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print("Please enter student number:");
int sno = sc.nextInt();
System.out.print("Please enter your grade:");
int grade = sc.nextInt();

System.out.println("===============");
System.out.println("[" + sno + "," + grade + "]");
}

Output result:

 

2. . if both receive the string type involved by the keyboard: [enter the keyboard to enter the next line, it is normal and normal, and there are no accidents such as error reporting]

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter your name:");
        String name = sc.nextLine();
        System.out.print("Please enter gender:");
        String sex = sc.nextLine();
        
        System.out.println("===============");
        System.out.println("[" + name + "," + sex + "]");
    }

 

 

 

3. Mixed situation when integer is first followed by string [there is a problem in the stage of entering name information]

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter student number:");
        int sno = sc.nextInt();
        System.out.print("Please enter your name:");
        String name = sc.nextLine();
        System.out.print("Please enter gender:");
        String sex = sc.nextLine();
        System.out.print("Please enter your grade:");
        int grade = sc.nextInt();
        
        
        System.out.println("===============");
        System.out.println("[" + sno + "," + name + "," 
        + sex + ","+ grade + "]");
    }

Output result:

 

Cause analysis:

When both are integers [. nextInt()], press enter after the integer entered by the keyboard is completed, and the line feed will be entered to enter the content of the next line, so there is no problem

When both are strings or the mixed case of integer first and then string [. nextLine()], you should clearly know that carriage return is also a string, which will be mistaken for a string and lead to bug s, such as in code 3

Enter the gender directly, the following results will appear, but if you type the string type of the second normal condition plus enter, there will be no problem!! Because after entering a string of characters nextLine() will absorb the carriage return character at the end,

Treat it as an end character and the cursor will move to the next line!

    

 

 

How to solve the above bug s? [carriage return is regarded as a string type, so the name cannot be entered!]

Method 1: in the face of integer first and then string type, first receive the integer typed by the keyboard, and then write one more line of code, {sc.nextLine(); [the function is to receive a single carriage return line feed, so that the subsequent input string types will not be affected]

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter student number:");
        int sno = sc.nextInt();
        sc.nextLine();   // Receive carriage return line feed to facilitate the typing of the next line
        System.out.print("Please enter your name:");
        String name = sc.nextLine();
        System.out.print("Please enter gender:");
        String sex = sc.nextLine();
        System.out.print("Please enter your grade:");
        int grade = sc.nextInt();
        
        
        System.out.println("===============");
        System.out.println("[" + sno + "," + name + "," 
        + sex + ","+ grade + "]");
    }

Output result:

 

Method 2: all keyboard input is received with string type. When integer type is required, it is ok to convert the string to integer type. For example, use the name variable of string type instead of integer type to receive, and then convert it to integer type again or re assign value to overwrite the [original stored value]

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String name = null; 
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter student number:");
        name = sc.nextLine();
        int sno = Integer.parseInt(name);
        System.out.print("Please enter your name:");
        name = sc.nextLine();
        System.out.print("Please enter gender:");
        String sex = sc.nextLine();
        System.out.print("Please enter your grade:");
        name = sc.nextLine();
        int grade = Integer.parseInt(name);
        
        
        System.out.println("===============");
        System.out.println("[" + sno + "," + name + "," 
        + sex + ","+ grade + "]");
    }

Output result:

 

Added by stiduck on Thu, 10 Mar 2022 05:54:54 +0200