Question: Reading multiple integers in multiple lines

Question

Reading multiple integers in multiple lines

Answers 4
Added at 2016-12-30 11:12
Tags
Question

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.

Answers
nr: #1 dodano: 2016-12-30 11:12

You are reading nextLine() in a loop after a single check for hasNextLine(). You need to perform a check after each read in order to avoid the "NoLineFound" exception.

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();
nr: #2 dodano: 2016-12-30 11:12

Just perform a check after reading to avoid the "NoLineFound" exception.

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();
        }
    }

}
nr: #3 dodano: 2016-12-30 11:12

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.

nr: #4 dodano: 2016-12-30 11:12

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);
Source Show
◀ Wstecz