7

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

最短路径(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)

为什么会发生此错误,如何纠正?

4

2 回答 2

3

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

一个简单的解决方法是在不重新安排代码的情况下结束 while 循环:

if (s.hasNextLine()) {
    s.nextLine();
}
于 2010-09-06T15:48:51.023 回答
0
    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)

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

最后的 2 个空行告诉我:线程“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]);

        }
    }
于 2010-09-06T15:37:28.323 回答