This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java scanner question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java scanner question" Watch "Java scanner question" New topic
Author

Java scanner question

cole relm
Greenhorn

Joined: Sep 16, 2011
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19726
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

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


It's not what your program can do, it's what your users do with the program.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

Joe is right about CSV files. See AccessingFileFormats for some more libraries, although I too have good experience with opencsv.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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.
 
 
subject: Java scanner question