[英]Reading multiple integers in multiple lines
I am currently writing my Bachelor's thesis in graph theory and use the java scanner to convert a txt file with edges into my Java class graph. 我目前正在用图论编写我的学士论文,并使用java扫描器将带边的txt文件转换为我的Java类图。 My txt file looks like this: 我的txt文件如下所示:
1 2 72 3 1 2 72 3
2 3 15 98 2 3 15 98
4 7 66 49 4 7 66 49
5 6 39 48 5 6 39 48
6 9 87 97 6 9 87 97
8 13 31 5 8 13 31 5
The ints are ordered as: source vertex, sink vertex, cost, capacity. 整数排序为:源顶点,汇点顶点,成本,容量。
My Code looks like: 我的代码如下:
Graph graph = new Graph(false);
File f = new File("Filepath");
Scanner in = new Scanner(f);
while (in.hasNextLine())
{
for (int i =1; i<= numberEdges; i++)
{
String s = in.nextLine();
try (Scanner inscan = new Scanner(s)) {
while (inscan.hasNext())
{
int source = inscan.nextInt();
int sink = inscan.nextInt();
double cost =inscan.nextDouble();
double capacity = inscan.nextDouble();
Vertex Source = new Vertex(source);
Vertex Sink = new Vertex(sink);
Edge edge = new Edge(Source,Sink, cost, capacity);
graph.addEdge(edge);
}
}
}
}
in.close();
I tried to scan each line in a String and then scan the String into my Variables. 我尝试扫描String中的每一行,然后将String扫描到我的变量中。 It always throws a "NoLineFound" Exception in the first line of the for loop and if I try it with outputing the lines I get none. 它总是在for循环的第一行抛出一个“NoLineFound”异常,如果我尝试输出这些行,我就得不到。 But when I disable the second scanner and try again I get all lines in the ouput but at the end still a "NoLineFound" Exception. 但是,当我禁用第二个扫描仪并再次尝试时,我获得输出中的所有行,但最后仍然是“NoLineFound”异常。
I checked my txt File and the last line doesn't have a UTF8 line ending, but I don't know how to give it one. 我检查了我的txt文件,最后一行没有UTF8行结束,但我不知道如何给它一个。
I think that your problem comes from that : 我认为你的问题来自于:
while (in.hasNextLine()){
for (int i =1; i<= numberEdges; i++)
{
First, iteration is redundant ( while
or for
are unitary enough for reading each line. You have to do choice between them). 首先,迭代是多余的( while
或者for
读取每一行都是单一的。你必须在它们之间做出选择)。
Besides if your file has less line than numberEdges
, a java.util.NoSuchElementException
will be raised. 此外,如果您的文件行数少于numberEdges
,则会引发java.util.NoSuchElementException
。
If the number of line is constant in the file, use a for
: 如果文件中的行数是常量,请使用for
:
for (int i =1; i<= numberEdges; i++)
remove the enclosing while (in.hasNextLine())
. 删除封闭的while (in.hasNextLine())
。 It is not required. 这不是必需的。 Iteration control is already done by the for
. 迭代控制已由for
完成。
If the number of lines in the file may vary, use only a while
: 如果文件中的行数可能不同,请仅使用一段while
:
while (in.hasNextLine()){
But anyway, don't use both. 但无论如何,不要同时使用两者。
With Java 8 streams: 使用Java 8流:
Files
.lines(f.toPath())
.map(l ->Arrays.stream(l.split(" ")).mapToDouble(Double::parseDouble).toArray())
.map(a->new Edge(new Vertex((int)a[0]), new Vertex((int)a[1]), a[2], a[3]))
.forEach(graph::addEdge);
You are reading nextLine()
in a loop after a single check for hasNextLine()
. 您正在阅读nextLine()
在一个循环中的一个检查后hasNextLine()
You need to perform a check after each read in order to avoid the "NoLineFound" exception. 您需要在每次读取后执行检查,以避免“NoLineFound”异常。
It looks like the nested loop is completely unnecessary. 看起来嵌套循环完全没必要。 You can read file line-by-line, ignoring empty lines, and build your graph without prior knowledge of the number of edges that it has: 您可以逐行读取文件,忽略空行,并在不事先了解其具有的边数的情况下构建图形:
Graph graph = new Graph(false);
File f = new File("Filepath");
Scanner in = new Scanner(f);
while (in.hasNextLine()) {
String s = in.nextLine();
try (Scanner inscan = new Scanner(s)) {
if (!inscan.hasNext()) {
continue; // Ignore empty lines
}
int source = inscan.nextInt();
int sink = inscan.nextInt();
double cost =inscan.nextDouble();
double capacity = inscan.nextDouble();
Vertex Source = new Vertex(source);
Vertex Sink = new Vertex(sink);
Edge edge = new Edge(Source,Sink, cost, capacity);
graph.addEdge(edge);
}
}
in.close();
Just perform a check after reading to avoid the "NoLineFound" exception. 只需在阅读后执行检查以避免“NoLineFound”异常。
You can use the below code to scan the file: 您可以使用以下代码扫描文件:
import java.io.File;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) {
try {
System.out.print("Enter the file name with extension : ");
Scanner input = new Scanner(System.in);
File file = new File(input.nextLine());
input = new Scanner(file);
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.