• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Reading a CSV file using scanner

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, i need to read a csv file using scanner but i am having issues when it goes to a new line

the input data is like this



the scanner is set like this



when i debug i can see that at the end of the line where there is \r\n for instance student number 3's last mark (92) is getting assigned to the same element in the array as the number four on the next line.

i think it has something to do with this line


but i am not sure how i could add the \r\n

as soon as it hits the \r\n i get


 
Saloon Keeper
Posts: 6054
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Saloon Keeper
Posts: 5718
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CSV has a number of edge cases which are not as simple to handle as it would first appear. I generally advise to use one of the many freely available libraries; see https://coderanch.com/wiki/660373/Accessing-File-Formats for a non-comprehensive list.
 
Doug Xander
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i worked around the issue using the following:

 
Tim Moores
Saloon Keeper
Posts: 5718
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that works for the files you're dealing with, cool. Just be aware that there are valid CSV files out there for which this will break.
 
Carey Brown
Saloon Keeper
Posts: 6054
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!