unkwndev
unkwndev

Reputation: 101

NoSuchElementException with scanner when reading csv file

So I've been trying to read in and parse data from a CSV File and I've got the data I needed from the file although when I print it, it gets real messy let me show you.

Here is the CSV file I am reading from

Liverpool Lime Street,P,B,D
Edge Hill,P,-,D
Mossley Hill,-,-,-
West Allerton,P,-,D
Liverpool South Parkway,-,-,D
Hunts Cross,P,-,-
Halewood,-,-,D
Hough Green ,-,-,D
Widnes ,P,B,-
Sankey for Penketh ,-,-,-
Warrington Central ,-,-,D
Padgate ,P,-,-
Birchwood ,-,-,D
Glazebrook,P,-,-
Irlam,-,B,-
Flixton,-,B,D
Chassen Road,P,-,D
Urmston ,P,B,D
Humphrey Park ,P,-,-
Trafford Park ,-,B,-
Deansgate ,P,-,D
Manchester Oxford Road ,-,B,D
Manchester Piccadilly ,P,-,D

Here is the Java code I have written to parse and display in the console

facilities.useDelimiter(",");
    while(facilities.hasNextLine()){
        String name = facilities.next();
        String parking = facilities.next(); 
        String bike = facilities.next();
        String disability = facilities.next();
        System.out.print("Name: " + name + " ");
        System.out.print("Parking: " + parking + " ");
        System.out.print("Bikes: " + bike + " ");
        System.out.print("disability: " + disability + " ");
        System.out.println();
   }

Here is a snippet of the result; where the problem lies...

Name: Liverpool Lime Street Parking: P Bikes: B disability: D
 Edge Hill 
 Name: P Parking: - Bikes: D
 Mossley Hill disability: - 
 Name: - Parking: -
 West Allerton Bikes: P disability: - 
 Name: D
 Liverpool South Parkway Parking: - Bikes: - disability: D
 Hunts Cross

The output should print the name parking bikes and disability and then a new line with the same data however if you look above the data is all over the place and poorly formatted. Why does facilities.next() just go to the next line when I never tell it to? Any help appreciated

Upvotes: 1

Views: 843

Answers (2)

Danny_ds
Danny_ds

Reputation: 11406

What's happening is that fields are read up to the next ,, even if that is on the next line. So for the first disability you get the string:

D\n
Edge Hill

and so on for the rest of the data.

Whereas you check for the existence of a 'next line', you don't enforce the parser to close the field at the end of the current line.

So to fix it the data should be processed line per line, or maybe you could tell the parser to use \n as a row separator.

Upvotes: 0

Domenick
Domenick

Reputation: 2432

In the code that you provided, you don't account for the newline character at the end of each line of input. I suggest, trying to skip the rest of the line (the newline character) after it reads the disability by adding: facilities.nextLine();

facilities.useDelimiter(",");
while(facilities.hasNextLine()){
    String name = facilities.next();
    String parking = facilities.next(); 
    String bike = facilities.next();
    facilities.nextLine();
    String disability = facilities.next();
    System.out.print("Name: " + name + " ");
    System.out.print("Parking: " + parking + " ");
    System.out.print("Bikes: " + bike + " ");
    System.out.print("disability: " + disability + " ");
    System.out.println();
}

Upvotes: 1

Related Questions