Read CSV with Scanner()
scanner.useDelimiter(",");
This should work.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("/Users/pankaj/abc.csv"));
scanner.useDelimiter(",");
while(scanner.hasNext()){
System.out.print(scanner.next()+"|");
}
scanner.close();
}
}
For CSV File:
a,b,c d,e
1,2,3 4,5
X,Y,Z A,B
Output is:
a|b|c d|e
1|2|3 4|5
X|Y|Z A|B|
Read CSV file with Scanner
Answering the question only, for education's sake...
First, if you want to skip the first field, simply do scanner.nextInt()
without assigning it to anything. scanner
here is essentially a cursor.
Second, if your delimiter is a semicolon (";"), then you need to specify that in the scanner.useDelimiter()
which you're current setting to a comma (",").
private static City parseInfo(String line) {
Scanner scanner = new Scanner(line);
scanner.useDelimiter("\\s*;\\s*");
scanner.nextInt();
String name = scanner.next();
String region = scanner.next();
String district = scanner.next();
int population = scanner.nextInt();
int foundation = scanner.nextInt();
return new City(name, region, district, population, foundation);
}
Reading CSV File Using Scanner Class Error
Your code fails when it tries to read the first balance. Since your delimiter is only a comma (,
). It tries to read 24.98<new-line>200
as a double.
We can use a comma and a new line character as delimiters using a Pattern
.
import java.util.regex.Pattern;
Pattern delimiter = Pattern.compile(",|\\s");
input = new Scanner(new File("clients.csv")).useDelimiter(delimiter);
There are some trailing spaces on a line of your files, so I used ,|\\s
otherwise you can use ,|\n
or ,|\n|\r\n
.
reading csv file using scanner in java
You can try Java lambda for reading lines of file, skip(1) to skip first line
//read file into stream, try-with-resources
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.skip(1).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
Creating a scanner that allows user to add CSV file to it in Java
What you can do with the Scanner is ask for the destination of the new CSV file, parse that to a string and use the FileReaders to read the contents of the file.
For example:
Scanner scanner = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter .csv filepath");
String filepath = scanner.nextLine();
List<String> contents = Files.readAllLines(Paths.get(filepath));
You however have to change some of this code to make it work. As the Paths.get(filepath)
only looks from the directory where you start the terminal (same folder).
When you run this when a file like text.txt
(or csv for that matter) with ./text.txt
as input it will get all the lines of the file and put it in the list.
Will this work for you?
NoSuchElementException with scanner when reading csv file
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();
}
Related Topics
Java - Process.Destroy() Source Code for Linux
Java Program That Runs Commands with Linux Terminal
Gson - Convert from JSON to a Typed Arraylist<T>
Can the Android Sdk Work with Jdk 1.7
Converting a String to an Integer on Android
Firebase Firestore Get Data from Collection
Retrofit2 Android: Expected Begin_Array But Was Begin_Object at Line 1 Column 2 Path $
How Do Jps, Jinfo, Jstat, Jmap and Jstack Get Information About Local Java Processes
Background Service for Android Oreo
What Happens When I Throw a C++ Exception from a Native Java Method
Onclicklistener Not Responding
How to View Visual Gc in Visualvm
How to Create a Bks (Bouncycastle) Format Java Keystore That Contains a Client Certificate Chain
How to Close Rmiregistry Running on Particular Port
How to Use Vectordrawable in Buttons and Textviews Using Android:Drawableright
Why Does Java Implicitly (Without Cast) Convert a 'Long' to a 'Float'