字符串扫描器在 Java 中不起作用

Scanner for string does not work in Java

当我使用 scan.nextLine() 时,输入框无法正常工作。如果是 scan.next() ,就可以 perfectly.But 为什么?我不应该对字符串使用 scan.nextLine() 吗?

import java.util.Scanner;
public class Test{
public static void main(String[] args){

    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();
    System.out.println("p");
    String p = scan.nextLine();
    System.out.println("q");
    String q = scan.next();
    System.out.println("m");
    String m = scan.next();
    }  
}

您应该使用 nextLine 然后将其转换为您期望的类型。

在上面的场景中,读取该行然后将其转换为整数,因为 nextnextInt 只是在 whitespace 发生之前读取输入。因此,当您调用 nextInt 时,它只会消耗数字并留下将在 nextLine.

中消耗的 newLine 字符

从问题来看,这似乎是您要读取输入的方式。

  1. 第一个整数。
  2. 第二个字符串行。
  3. 第三行将有两个单词由 space 分隔。

这就是您的代码。

    Scanner scan = new Scanner(System.in);
    int x = Integer.parseInt(scan.nextLine()); //read line and then cast to integer
    System.out.println("p");
    String p = scan.nextLine();
    System.out.println("q m");
    String[] linesParts = scan.nextLine().split(" "); // read the whole line and then split it.
    String q = linesParts[0];
    String m = linesParts[1];

这是我要使用的问题的解决方案。 Tahir Hussain Mir 的上述评论可能是问题的原因

import java.util.ArrayList;
import java.util.Scanner;

public class app {
public static void main(String[] args) {
    // declare scanner
    Scanner scan = new Scanner(System.in);

    // what ever number you need, it could be calculated
    int numberOfInputLines = 3;

    // the list of the lines entered
    ArrayList<String[]> list = new<String[]> ArrayList();

    // add each line to the list
    for (int i = 0; i < numberOfInputLines; i++) {
        // get entire line as a single string
        String input = scan.nextLine();
        // split the line into tokens, and store the array in the array list
        String[] result = input.split("\s");
        list.add(result);

    }

    // iterate through each line
    for (int i = 0; i < list.size(); i++) {

        // iterate through the line
        for (int j = 0; j < list.get(i).length; j++) {
            if (isInteger(list.get(i)[j]) == true) {
                // do what you want if the input is an int
                //to show it works
                System.out.println("int: " + list.get(i)[j]);

            } else {
                // do what you want if a the token inputed is a string
                //to show it works
                System.out.println("String: " + list.get(i)[j]);

            }

        }

    }

}

// greasy way to check if is an int
private static boolean isInteger(String s) {
    try {
        Integer.parseInt(s);
    } catch (NumberFormatException e) {
        return false;
    } catch (NullPointerException e) {
        return false;
    }
    // only got here if we didn't return false
    return true;
}

}

在使用它们之前,请尝试查看文档。
原因:

Scanner.nextLine : The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

虽然,这不适用于 Scanner.nextInt

因此,Scanner.nextInt 方法不会使用您输入的最后一个换行符,因此在下一次调用 Scanner.nextLine.

时会使用该换行符

基本 解决方案 将在 Scanner.nextInt 之后使用空白 Scanner.nextLine 来消耗该行的其余部分,包括换行符。

例如

int myVal1 = input.nextInt();
input.nextLine(); 
String myStr1 = input.nextLine();