【问题标题】:Scanner for string does not work in Java [duplicate]字符串扫描仪在 Java 中不起作用 [重复]
【发布时间】:2017-04-09 10:16:03
【问题描述】:

当我使用 scan.nextLine() 时,输入框无法正常工作。如果是 scan.next() ,效果很好。但是为什么呢?我不应该使用 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();
    }  
}

【问题讨论】:

  • 可以显示输入格式吗?
  • 添加scan.useDelimiter(System.getProperty("line.separator"));
  • 我的建议是只调用一次 String p = scan.nextLine();对于每一行。然后分析字符串,分解成你需要的组件。

标签: java string java.util.scanner next drjava


【解决方案1】:

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

Scanner.nextLine : java.util.Scanner.nextLine() 方法将此扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。由于此方法继续搜索输入以查找行分隔符,因此如果不存在行分隔符,它可能会缓冲所有搜索要跳过的行的输入。

虽然,这不适用于 Scanner.nextInt

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

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

举例

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

【讨论】:

    【解决方案2】:

    这是我要使用的问题的解决方案。 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;
    }
    
    }
    

    【讨论】:

      【解决方案3】:

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

      在上述场景中,读取该行然后将其转换为整数,因为nextnextInt 只是在出现空白之前读取输入。因此,当您调用nextInt 时,它只会使用该号码并留下newLine 字符,该字符将在nextLine 中使用。

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

      1. 第一个整数。
      2. 第二个字符串行。
      3. 第三行有两个用空格分隔的单词。

      这就是你的代码应该是的。

          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];
      

      【讨论】:

      • 我为什么要这样做?对此答案也有反对意见。
      • 投反对票的人也请离开 cmets。谢谢
      • 没错,投反对票的请离开 cmets。
      • 我正要投票以节省您的代表,但我只是尝试测试您的代码,并且在您输入输入时它会崩溃。另外,我不是投反对票的人
      • @holycatcrusher 好吧我在代码之前提到了输入格式,如果是那种格式,它不会崩溃。
      猜你喜欢
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多