Reading a CSV with Scanner Example in Java

preview_player
Показать описание
In this video, I give an example of how to read a CSV file with the Scanner class in Java.

Рекомендации по теме
Комментарии
Автор

Thank you very much. I was missing the pipe CR to make it work.

rul
Автор

Thanks for the great video. It was helpful. One question: what happens when the file I am reading has say name, profession, age for multiple people but the delimiter can be one space, two spaces...or a tab...so what to do if the delimiter is not a constant space ?
ANYONE PROFESSOR 55
SOMEONE TEACHER 23
try {
String name, profession, age;
Scanner sc = new Scanner(f);
sc.useDelimiter(" | | | | | |\n");

while(sc.hasNextLine()){
name = sc.next();
profession = sc.next();
age = sc.next();


System.out.println(name + " "+ profession +" "+ age);
}
sc.close();
I get NO SUCH ELEMENT Exception! All right, got it... If the number of spaces exceeds what I specify in sc.useDelimiter(" | | | | | |\n"); then I get an error...so is there a better way? Thanks

whimsicalvibes
visit shbcf.ru