使用 Scanner 的 nextLine() 和 hasNextLine() 方法时出现问题

发布于 2024-09-18 07:20:43 字数 1358 浏览 6 评论 0原文

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

最短路径(2)::RV3280-RV0973C-RV2888C
最短路径(1)::RV3280-RV2502C
最短路径(2)::RV3280-RV2501C-RV1263
最短路径(2)::RV2363-Rv3285-RV3280

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

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

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");
        }

我得到了想要的结果,但也收到了一条错误消息。我从上述输入文件得到的输出是:

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)

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-09-25 07:20:43

您的输入数据可能不以行分隔符结尾,这会导致这种情况。调用 findInLine 会将扫描器移过匹配模式,如果在调用 nextLine 时位于输入数据的末尾,它将抛出 NoSuchElementException

无需过多重新安排代码的简单修复方法是通过以下方式结束 while 循环:

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

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();
}
草莓味的萝莉 2024-09-25 07:20:43
    public static void main(String[] args) {
            Scanner s = new Scanner("Shortest path(2)::RV3280-RV0973C-RV2888C"
                    + "\nShortest path(1)::RV3280-RV2502C"
                    + "\nShortest path(2)::RV3280-RV2501C-RV1263"
                    + "\nShortest path(2)::RV2363-Rv3285-RV3280");
            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();
    }
}

run:
2
RV3280
RV2888C
1
RV3280
RV2502C
2
RV3280
RV1263
2
RV2363
RV3280
BUILD SUCCESSFUL (total time: 0 seconds)

这对我来说效果很好,也许你的文件中有一些奇怪的字符或空行?

最后两行空行告诉我:
线程“main”中的异常 java.lang.IllegalStateException:没有可用的匹配结果

如果您的输入文件格式严格,您可以执行类似的操作,这更容易,因为您可以摆脱那个令人讨厌的正则表达式;)

    String[] lines = new String[]{"Shortest path(2)::RV3280-RV0973C-RV2888C", "Shortest path(1)::RV3280-RV2502C", "Shortest path(2)::RV3280-RV2501C-RV1263", "Shortest path(2)::RV2363-Rv3285-RV3280", "\n", "\n"};
    final int positionOfIndex = 14;
    final int startPositionOfProteins = 18;
    for (String line : lines) {
        if (!line.trim().isEmpty()) {
            System.out.print(line.charAt(positionOfIndex) + ": ");
            String[] proteins = line.substring(startPositionOfProteins).split("-");
            System.out.println(proteins[0] + " " + proteins[proteins.size() -1]);

        }
    }
    public static void main(String[] args) {
            Scanner s = new Scanner("Shortest path(2)::RV3280-RV0973C-RV2888C"
                    + "\nShortest path(1)::RV3280-RV2502C"
                    + "\nShortest path(2)::RV3280-RV2501C-RV1263"
                    + "\nShortest path(2)::RV2363-Rv3285-RV3280");
            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();
    }
}

run:
2
RV3280
RV2888C
1
RV3280
RV2502C
2
RV3280
RV1263
2
RV2363
RV3280
BUILD SUCCESSFUL (total time: 0 seconds)

This works well for me, maybe you have some weird characters or empty lines in your file?

2 empty lines at the end give me that:
Exception in thread "main" java.lang.IllegalStateException: No match result available

If your input file is that strictly formatted, you can do something like that, which is way easier because you can get rid of that nasty regex ;)

    String[] lines = new String[]{"Shortest path(2)::RV3280-RV0973C-RV2888C", "Shortest path(1)::RV3280-RV2502C", "Shortest path(2)::RV3280-RV2501C-RV1263", "Shortest path(2)::RV2363-Rv3285-RV3280", "\n", "\n"};
    final int positionOfIndex = 14;
    final int startPositionOfProteins = 18;
    for (String line : lines) {
        if (!line.trim().isEmpty()) {
            System.out.print(line.charAt(positionOfIndex) + ": ");
            String[] proteins = line.substring(startPositionOfProteins).split("-");
            System.out.println(proteins[0] + " " + proteins[proteins.size() -1]);

        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文