【问题标题】:Printing elements from a file using Scanner from Java使用 Java 中的 Scanner 从文件中打印元素
【发布时间】:2019-03-05 02:40:34
【问题描述】:

您好,我需要 Java 方面的帮助。因为我一直在尝试使用扫描仪从文件中输出行,但我没有成功。这是文件内容的样子:

4
5
3->1->25
1->0->12
2->0->-5
0->1->0
2->1->7

我尝试做的第一件事是从第三行开始输出信息,效果很好。这是代码:

public static void main(String[] args) throws Exception {
    Scanner scanner = null;
    BufferedReader in = null;
    String line = null;
    try{
        scanner = new Scanner(new BufferedReader(new FileReader("graphe.txt")));
        //in = new BufferedReader(new FileReader("graphe.txt"));
        scanner.useDelimiter("->");
        while (scanner.hasNextLine()){

            int value = scanner.nextInt();
            scanner.skip(scanner.delimiter());
            int value2 = scanner.nextInt();
            scanner.skip(scanner.delimiter());
            String value3 = scanner.nextLine();
            int value3Int = Integer.parseInt(value3);
            System.out.println(value + " - > " + value2 + " cost " + value3Int);
        }

    }catch (IOException e){
        e.printStackTrace();
    }finally {
        if (scanner != null){
            scanner.close();
        }
    }

}

}

但是当我插入值 4 和(第一行和第二行)时,我试图弄清楚如何处理它。我做的第一件事是尝试使用 if 条件来查看分隔符是否存在,如果不存在,我将其打印出来:

public static void main(String[] args) throws Exception {
    Scanner scanner = null;
    BufferedReader in = null;
    String line = null;
    try{
        scanner = new Scanner(new BufferedReader(new FileReader("graphe.txt")));
        //in = new BufferedReader(new FileReader("graphe.txt"));
        scanner.useDelimiter("->");
        while (scanner.hasNextLine()){

            String find = scanner.nextLine();
            if (!(find.contains("->"))){
                System.out.println(find);
            }
            else {
                int value = scanner.nextInt();
                scanner.skip(scanner.delimiter());
                int value2 = scanner.nextInt();
                scanner.skip(scanner.delimiter());
                String value3 = scanner.nextLine();
                int value3Int = Integer.parseInt(value3);
                System.out.println(value + " - > " + value2 + " cost " + value3Int);
            }
        }

    }catch (IOException e){
        e.printStackTrace();
    }finally {
        if (scanner != null){
            scanner.close();
        }
    }

}

}

但它并没有像预期的那样工作,我有这个错误作为输出

4

5

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)

1 - > 0 cost 12

    at java.base/java.util.Scanner.next(Scanner.java:1594)

0 - > 1 cost 0

    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at com.tpTG.LectureGrapheMatrice.main(LectureGrapheMatrice.java:25)

Process finished with exit code 1

请我知道我可以处理它,但我一直在思考,但我不知道该怎么做。请帮忙谢谢。

【问题讨论】:

  • 你要忽略4和5还是别的什么?
  • 不,我想打印所有这些行
  • 你不知道这些单数行有多少,对吧?
  • 对不起?请我不明白你想说什么。但我要说的是我想打印出文件中的所有行
  • 一旦您阅读了带有scanner.nextLine() 的行并确定它有一个分隔符,您就开始调用scanner.next()scanner.skip() 来尝试剖析它。但此时,scanner.next()scanner.skip() 现在正在从 next 行读取。

标签: java java.util.scanner


【解决方案1】:

我认为每行的正则表达式 replaceAll 可能会做到这一点。

// no need to set delimiter
while (scanner.hasNextLine()) {
    String line = scanner.nextLine().replaceAll("^((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)$", 
                                                "$1 -> $2 costs $3");
    System.out.println(line);
}

如果输入与模式匹配,则将其格式化为“x -> y cost z”,否则replaceAll 将不执行任何操作并输出同一行。

如果你想要这三个值,你可以使用Matcher.group访问捕获的值,

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    Matcher m = Pattern.compile("((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)").matcher(line);
    if (m.matches()) {
        // m.group(1), m.group(2) and m.group(3) are the three values as strings. You can convert them to ints yourself.
        System.out.println(m.group(1) + " -> " + m.group(2) + " costs " + m.group(3));
    } else {
        System.out.println(line);
    }
}

【讨论】:

  • 非常感谢!它工作得很好。但如果你不介意我只是想知道我的代码有什么问题?
  • @The_Thinker 正如凯文安德森所说的那样。在nextLine 之后调用nextInt。您扫描的 int 不会是您扫描的行中的 int,因为扫描仪已经扫描了该行并继续前进。您需要从find 中提取int,而不是扫描新的int。
  • 是的,但我还不知道如何构建它。因为稍后我想处理那些值: x -> y 成本 z 其中 x 将是行 y 列和 z 的值 (x,y)
  • 我会用你的正则表达式解决方案试一试,但我的我仍在努力解决,正如@Kevin Anderson 所说的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多