Sc.nextLine() and sc.nextInt() in idea deal with the problem of carriage return and line break

Article directory

  • 1. Find the problem
    • 1. sc. nextLine()
    • 2. sc. nextInt()
  • 2. Analyzing the problem
  • 3. Summary
  • 4. New issues
    • 1.
    • 2.

1. Found the problem

Today, when I checked the questions in Luogu, I found that the string could not be entered, and then I came up with today’s article. Look at the following question first.

1. sc. nextLine()

package Luogu;

import java.util.*;

public class Test {<!-- -->

    public static void main(String[] args){<!-- -->
        Scanner sc = new Scanner(System.in);

        String str1 = sc. nextLine();
        String str2 = sc. nextLine();

        System.out.println(str1);
        System.out.println(str2);

    }

}

Pretty basic, right, but the results are amazing, let’s take a look at the results of the execution.

Here I entered the strings “1” and “2”. When printing, only “1” is printed. Why, let’s debug it. I saw the following results:

str2 did not read the “2” it should have. Guess: If it is not read into a different character? Let’s modify the code as follows:

package Luogu;

import java.util.*;

public class Test {<!-- -->

    public static void main(String[] args){<!-- -->
        Scanner sc = new Scanner(System.in);

        String str1 = sc. nextLine();
        sc. nextLine();
        String str2 = sc. nextLine();

        System.out.println(str1);
        System.out.println(str2);

    }

}

The blogger added a sc.nextLine() here to read other characters

This time str2 holds the correct value and prints it out successfully.

2. sc. nextInt()

package Luogu;

import java.util.*;

public class Test {<!-- -->

    public static void main(String[] args){<!-- -->
        Scanner sc = new Scanner(System.in);

        int num = sc. nextInt();

        String str = sc. nextLine();

        System.out.println(num);
        System.out.println(str);

    }

}

I feel that there is nothing wrong with it. It turned out to be running. Is it this? As shown below:

Just let me enter a num, and before I had time to enter it, it was over. Let’s debug it, as shown below:

As in the above case, str is still not entered, and there is one null value. That’s the same as above, we also add a sc.nextLine() to see how the result is:

package Luogu;

import java.util.*;

public class Test {<!-- -->

    public static void main(String[] args){<!-- -->
        Scanner sc = new Scanner(System.in);

        int num = sc. nextInt();
        sc. nextLine();
        
        String str = sc. nextLine();

        System.out.println(num);
        System.out.println(str);

    }

}

The result of the operation is as follows:

This time I was asked to enter str, but it seems that it is still not saved. Why is this a bit like the situation in sc, nextLine() above. . . debug again:

Still haven’t read it in, well, let’s add another sc.nextLine() to try, the code is as follows:

package Luogu;

import java.util.*;

public class Test {<!-- -->

    public static void main(String[] args){<!-- -->
        Scanner sc = new Scanner(System.in);

        int num = sc. nextInt();
        sc. nextLine();
        sc. nextLine();
        
        String str = sc. nextLine();

        System.out.println(num);
        System.out.println(str);

    }

}

This time the operation was successful, as shown below:

2. Analyzing the problem

Why does this happen? I checked the information and found:

After pressing the Enter key on the keyboard, two characters are actually sent to the program ———‘\r’ and ‘\

  • sc.nextInt(), after entering the number, both ‘\r’ and ‘\
    ‘ are left in the buffer.

    • After sc.nextInt(), two nextLine() are needed to handle other characters.
  • And sc.nextLine(), after entering the string, only ‘\
    ‘ remains in the buffer.

    • After sc.nextLine(), a nextLine() is needed to handle other characters.

Three. Summary

  • In the Unix system, the end of each line is only ““, that is, “\
    “; In the Windows system, the end of each line is “, that is, “\r\ n”; in the Mac system, the end of each line is ““, that is, “\r”
  • ‘\r’means carriage return (carriage return), that is, returning to the beginning of the line, and does not include the action of line break;’\
    ‘means line feed (line feed), that is, moves to a new line (next line)
  • nextInt(), next(), nextFloat(), and nextDouble() all only read valid characters, and will not read space bar, tab key and enter key.
    • It will auto-skip when it encounters these invalid characters before entering valid characters, and after valid characters End reading, leaving invalid characters in buffer.
  • nextLine() can read everything except the carriage return, that is, the reading ends when encountering the carriage return, but the carriage return will be left in the buffer.

refer to:
https://blog.csdn.net/cnds123/article/details/126389380
https://blog.csdn.net/weixin_55000908/article/details/123888389
https://blog.csdn.net/m0_63162560/article/details/126211023

Four. New questions

1.

package Luogu;

import java.util.*;

public class Test {<!-- -->

    public static void main(String[] args){<!-- -->
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the content you want to print:");
        String info1 = sc. nextLine();
        System.out.println("The digital data entered by the keyboard is: " + info1);
        String info2 = sc. nextLine();
        String info3 = sc. nextLine();
        System.out.println("The string data entered by the keyboard is: " + info2);
        System.out.println("The digital data entered by the keyboard is: " + info3);


    }

}

The result is as follows:

Here info2 actually read in? ? ? ? Stumped System.out.println (); has the effect of clearing the temporary storage area? The blogger read the source code for a long time, but didn’t understand it. . . Maybe it’s too good

2.


There is also such a piece of code, where sc.nextLine() is added, but it will wait for input. . It’s puzzling.

If anyone knows, please feel free to enlighten me, clap your fists!