gpt4 book ai didi

java - 使用来自 Java 的扫描仪打印文件中的元素

转载 作者:行者123 更新时间:2023-11-30 10:11:08 28 4
gpt4 key购买 nike

您好,我需要一些 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

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

最佳答案

我认为每一行的正则表达式 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 costs 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);
}
}

关于java - 使用来自 Java 的扫描仪打印文件中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566515/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com