(java)scanner.next() and scanner The difference between nextline()

Posted by genics on Mon, 21 Feb 2022 10:49:22 +0100

Brush the programming questions on niuke.com. If you use java language for programming, the most common input is to use Scanner. Its structure is very simple:

Scanner sc = new Scanner(System.in);

Then you can use sc.next() and sc.nextLine() to get the user input information. By default, the string information is obtained.

Generally, we think that the user input is a string, but in a specific case, we can think that the user input is an integer number, so we can use sc.nextInt() to obtain the integer entered by the user.

Although both sc.next() and sc.nextLine() can obtain the information entered by the user, sc.next() is to obtain the first space entered by the user or the string before carriage return. If it is carriage return, the carriage return character will be left, which creates a pit. If there is input later, we will read an empty string instead of the newly entered string.

There is a topic on niuke.com, which says that first read an integer n, then read n strings and sort the N strings.

The java code is as follows:

import java.util.Arrays;
import java.util.Scanner;

public class StringArraySortTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
		    int size= sc.nextInt();
			String[] arr = new String[size];
			for(int i=0;i<size;i++) {
				arr[i] = sc.nextLine();
			}
			Arrays.sort(arr);
			for(String s:arr) {
				System.out.println("["+s+"]");
			}
		}
		sc.close();
	}
}

Screenshot of program operation:

Originally, I wanted to input integer 3, and then input three strings. When I input two strings, the program will calculate and print the result. By adding special print characters, we find that the first one is actually empty, that is, sc.nextInt() only reads numbers and leaves the last carriage return character of the line. Therefore, when you are ready to input three lines of strings later, this empty will be the first string by default.

To make this program as we expected, we can modify two places:

1. When reading an integer for the first time, use the line method to read sc.nextLine(), and then use integer Valueof() turn around so that no carriage return is left.

2. If you directly read the integer with sc.nextInt(), then when you read the string later, use sc.next() instead of sc.nextLine() by reading the line.

The codes of both methods are posted:

import java.util.Arrays;
import java.util.Scanner;

public class StringArraySortTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int size = Integer.valueOf(sc.nextLine());
		    //int size= sc.nextInt();
			String[] arr = new String[size];
			for(int i=0;i<size;i++) {
				arr[i] = sc.nextLine();
			}
			Arrays.sort(arr);
			for(String s:arr) {
				System.out.println("["+s+"]");
			}
		}
		sc.close();
	}
}

All use sc.next() encoding:

import java.util.Arrays;
import java.util.Scanner;

public class StringArraySortTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
		    int size= sc.nextInt();
			String[] arr = new String[size];
			for(int i=0;i<size;i++) {
				arr[i] = sc.next();
			}
			Arrays.sort(arr);
			for(String s:arr) {
				System.out.println("["+s+"]");
			}
		}
		sc.close();
	}
}

Screenshot of the print result of running the program correctly:

In fact, the content of this article is based on the solution results of the programming problem on niuke.com.  

 

Topics: Java Back-end scanner