【问题标题】:scanNextInt() ignore empty linesscanNextInt() 忽略空行
【发布时间】:2012-12-05 14:24:25
【问题描述】:

我使用这个代码:

public String processFile(Scanner scanner) {
    String result = "";
    SumProcessor a = new SumProcessor();
    AverageProcessor b = new AverageProcessor();
    String line = null;
    while (scanner.hasNext()) {

        if (scanner.hasNext("avg") == true) {

            c = scanner.next("avg");
           while(scanner.hasNextInt()){

                int j = scanner.nextInt();
                a.processNumber(j);
            }
           System.out.println("Exit a");
            result += a.getResult();
            a.reset();
        }
        if (scanner.hasNext("sum") == true) {

            c = scanner.next("sum");
           while(scanner.hasNextInt()){

          int j = scanner.nextInt();
                b.processNumber(j);                    
            }
            System.out.println("Exit b");
             result += b.getResult();
             b.reset();
        }

    }
    return result;
}

当我按下回车或发送空行时,我需要在循环 (hasNexInt()) 时结束。

我尝试使用 String == null e.t.c. 的一些方法,但 Java 只是 IGNORE 空行

输出

run:
avg
1
2
3
4

sum
Exit a
1
2
3
4

但我需要:

run:
avg
1
2
3
4

Exit a
sum    
1
2
3
4

【问题讨论】:

    标签: java


    【解决方案1】:

    如果不是绝对必须在您的应用中使用扫描仪,我可以提供:

    BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
    for(;;) {
        String lile = rdr.readLine();
        if (lile.trim().isEmpty()) {
            break;
        }
        // process your line
    }
    

    此代码肯定会在控制台的空行处停止。现在您可以使用 Scanner 进行行处理或正则表达式。

    【讨论】:

      【解决方案2】:

      只需使用类似的东西:

      String line = null;
      while(!(line = keyboard.nextLine()).isEmpty()) {
      // Your actions
      }
      

      【讨论】:

        【解决方案3】:

        在第二个 while 循环中使用 hasNextInt()。当您没有传递 int 值时,while 循环将中断。

        或者你也可以确定一个特定的值,你可以传递它来打破循环。例如,您可以传递字符'x',然后检查是否传递了'x' -> 打破循环。

        while (scanner.hasNext()) {
                if (scanner.hasNext("avg") == true) {
        
                    c = scanner.next("avg");
                   while (scanner.hasNextInt()){ //THIS IS WHERE YOU USE hasNextInt()                       
                        int j = scanner.scanNextInt();
                        a.processNumber(j);
                    }
                   System.out.println("End While");
                    result += a.getResult();
                    a.reset();
                }
        

        【讨论】:

        • 我试试这个 :) 查看我的 FIXed 代码。但只有当我在avg 之后输入sum 或在sum 之后输入avg 时才会中断
        • @VladislavIl'ushin 你试过 String.equals("") 吗?我想我明白你在做什么。只需在控制台输入中输入一个空字符串,然后在您的 while 循环中检查您的字符串是否等于(“”)。如果是,则中断循环。希望这些信息对您有所帮助!
        【解决方案4】:

        只需添加scanner.nextLine() 以忽略该行中的其余条目:

                    while (scanner.hasNextLine()){
                        String line = scanner.nextLine();
                        if("".equals(line)){
                           //exit out of the loop
                           break;
                        }
                        //assuming only one int in each line
                        int j = Integer.parseInt(line);
                        a.processNumber(j);
                    }
        

        【讨论】:

        • 我不需要忽略空行,我不需要忽略空行
        • @VladislavIl'ushin 我的意思是,它会忽略您与 nextInt() 一起输入的换行符。在最后忽略换行符之前,我进一步改进了从行中读取所有 int 类型输入的答案。
        • @VladislavIl'ushin 它有帮助还是仍然存在问题?如果有问题,您能否强调一下,什么不起作用?
        • 我需要结束 While 循环 INSIDE,因为我在 Inside WHILE 中断后处理数字
        • @VladislavIl'ushin 但是你的 processNumber 方法只接受一个 int 作为参数。
        猜你喜欢
        • 2022-09-23
        • 2013-01-25
        • 1970-01-01
        • 2021-08-21
        • 2012-04-22
        • 2019-10-15
        • 2014-03-27
        • 2019-02-27
        • 1970-01-01
        相关资源
        最近更新 更多