• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java scanner question

 
cole relm
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I make a scanner.useDelimiter(",") use a comma for a delimiter as well as a new line? I have a .CSV file which I need separated at the commas but at the end of each line there is white space.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The delimiter is a regular expression. With the help of the Javadoc page of java.util.regex.Pattern you should be able to get a start on creating the right one.
 
Joe Areeda
Ranch Hand
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mean to confuse the issue but csv files can get pretty complicated if you have quoted strings (which can contain commas) and escaped characters (like quotes).

There are several packages out there that handle all the complications. OpenCSV is the one I use.

If you are using this to learn Java, never mind.

Joe
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe is right about CSV files. See AccessingFileFormats for some more libraries, although I too have good experience with opencsv.
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try the String#trim() method to get rid of the whitespace, but if there is a good library already available, you should not try reinventing the wheel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic