使用Scanner的nextLine()和hasNextLine()方法时出现问题 [英] Problem using the nextLine() and hasNextLine() methods of Scanner

查看:688
本文介绍了使用Scanner的nextLine()和hasNextLine()方法时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的日志文件:



最短路径(2):: RV3280-RV0973C-RV2888C

最短路径( 1):: RV3280-RV2502C

最短路径(2):: RV3280-RV2501C-RV1263

最短路径(2):: RV2363-Rv3285-RV3280



从每一行开始,我需要括号内的数字,第一个蛋白质的名称(第一行中的RV3280)和最后一个蛋白质的名称(第一行中的RV2888C)。 / p>

我使用 Scanner 对象为此编写了代码。

 试试{
Scanner s = new Scanner(new File(args [0]));
while(s.hasNextLine()){
s.findInLine(最短路径\\((\\\\ +)\\)::(\\\\ + +) 。* - (\\w +)); //在每一行,寻找这种模式
MatchResult result = s.match(); //来自
的结果(int i = 1; i< = result.groupCount(); i ++){
System.out.println(result.group(i));
}
s.nextLine(); //行号29
}
s.close();
}

catch(FileNotFoundException e){
System.out.print(找不到文件);
}

我得到了预期的结果,但我也收到错误信息。上面输入文件的输出是:

 线程main中的异常java.util.NoSuchElementException:找不到行
at java.util.Scanner.nextLine(Scanner.java:1516)
at nearnessindex.Main.main(Main.java:29)
2
RV3280
RV2888C
1
RV3280
RV2502C
2
RV3280
RV1263
2
RV2363
RV3280
Java结果:1
BUILD SUCCESSFUL(总时间:1秒)

为什么会出现此错误会发生什么以及如何纠正它?

解决方案

您的输入数据可能不会以行分隔符结束,这会导致此问题。调用 findInLine 会使扫描程序超出匹配模式,如果您在调用 nextLine 时位于输入数据的末尾它将抛出 NoSuchElementException



一个简单的修复,无需重新安排代码就可以结束循环:

  if(s.hasNextLine()){
s.nextLine();
}


I have a log file containing the following data:

Shortest path(2)::RV3280-RV0973C-RV2888C
Shortest path(1)::RV3280-RV2502C
Shortest path(2)::RV3280-RV2501C-RV1263
Shortest path(2)::RV2363-Rv3285-RV3280

From each line, i require the number within the brackets, name of the first protein (RV3280 in the first line) and the name of the last protein (RV2888C in the first line).

I have written a code for this using the Scanner object.

try{
                Scanner s = new Scanner(new File(args[0]));
                while (s.hasNextLine()) {
                    s.findInLine("Shortest path\\((\\d+)\\)::(\\w+).*-(\\w+)"); // at each line, look for this pattern
                    MatchResult result = s.match(); // results from
                    for (int i=1; i<=result.groupCount(); i++) {
                        System.out.println(result.group(i));
                    }
                    s.nextLine(); // line no. 29
                }
                s.close();
        }

        catch (FileNotFoundException e) {
            System.out.print("cannot find file");
        }

I get the desired results but i also get an error message. The output i get for the above input file is:

Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Scanner.java:1516)
        at nearnessindex.Main.main(Main.java:29)
2
RV3280
RV2888C
1
RV3280
RV2502C
2
RV3280
RV1263
2
RV2363
RV3280
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Why does this error occur and how can correct it?

解决方案

Your input data probably doesn't end with a line separator which would cause this. Calls to findInLine moves the Scanner past the matching pattern and if you are at the end of the input data when calling nextLine it will throw the NoSuchElementException

A easy fix without re-arranging the code to much would be to end the while loop with:

if (s.hasNextLine()) {
    s.nextLine();
}

这篇关于使用Scanner的nextLine()和hasNextLine()方法时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆