wood burning stoves 2.0*
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
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: 19653
    
  18

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: 307
    
    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: 19653
    
  18

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: 38016
    
  22
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.
 
Consider Paul's rocket mass heater.
 
subject: Java scanner question
 
Similar Threads
Anonymous class behavior confusion !
If conditional not branching correctly
WA #2 ..... word association
What if i want a return
what is the error in this program