5

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.

Tom
  • 16,842
  • 17
  • 45
  • 54
0lisa0
  • 73
  • 1
  • 6
  • can you please add the exact code you have for `File f = new File("Filepath");` – Sanjit Kumar Mishra Dec 30 '16 at 10:11
  • Look at the accepted answer of this question http://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found . You need to put a check `in.hasNextLine()` before doing `nextLine()` – Shubham Chaurasia Dec 30 '16 at 10:11
  • I see no sense in the for-loop. Have a try without it – Turo Dec 30 '16 at 10:15
  • Thanks! the problem was the for-loop. I just started using scanner yesterday and didn't realize that while(in.hasnextline()) does the same as my for loop – 0lisa0 Dec 30 '16 at 10:28

4 Answers4

6

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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
6

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);
Sergey Fedorov
  • 2,169
  • 1
  • 15
  • 26
2

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();
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

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

}
Sanjit Kumar Mishra
  • 1,153
  • 13
  • 32