File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java scanner question" Watch "Java scanner question" New topic

Java scanner question

cole relm

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

Joined: Oct 27, 2005
Posts: 20279

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.

How To Ask Questions How To Answer Questions
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 331

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.


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

Joined: Oct 27, 2005
Posts: 20279

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

Joined: Oct 13, 2005
Posts: 46412
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.
I agree. Here's the link:
subject: Java scanner question
It's not a secret anymore!