Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. My txt file looks like this:

1 2 72 3
2 3 15 98
4 7 66 49
5 6 39 48
6 9 87 97
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. 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. 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.

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.

like image 480
0lisa0 Avatar asked Dec 30 '16 10:12

0lisa0


2 Answers

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).
Besides if your file has less line than numberEdges, a java.util.NoSuchElementException will be raised.

If the number of line is constant in the file, use a for:

for (int i =1; i<= numberEdges; i++)

remove the enclosing while (in.hasNextLine()). It is not required. Iteration control is already done by the for.

If the number of lines in the file may vary, use only a while :

    while (in.hasNextLine()){

But anyway, don't use both.

like image 182
davidxxx Avatar answered Sep 30 '22 07:09

davidxxx


With Java 8 streams:

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);
like image 41
Sergey Fedorov Avatar answered Sep 30 '22 08:09

Sergey Fedorov