This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Ignoring 1 column and reading the other using a Scanner object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ignoring 1 column and reading the other using a Scanner object" Watch "Ignoring 1 column and reading the other using a Scanner object" New topic
Author

Ignoring 1 column and reading the other using a Scanner object

Timothy Hoyle
Greenhorn

Joined: Oct 12, 2009
Posts: 18
Okay, so this is my predicament, I have two columns in a text file that I want to read. They are for a budget program, so they go like this:

Col 1 Col 2

Savings $100.00
Groceries $100.00
Gas $150
etc. etc.

I want to read these values back into my budget program using a Scanner. However, I only want to read the second column, I want the scanner to skip the first column, always, because I want the numeric values read back into my budget application. Is there some way I can use nextLine() or next() then skip, then nextLine(), then skip, and so on?

Maybe a link explaining the process would work.

Thanks in advance.

Tim Hoyle
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
I don't know, but you could split and get the second column.

Maybe something like this:

nextLine().split(" ")[1];
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
You can call next() and discard the result, but that will only work if you are sure about how many tokens you get in each line.
You could split on the dollar sign, then call nextDouible(). Remember you pass the delimiter to the Scanner as a regular expression, so you could use dollar, but that is a meta-character for last character in line, so may have to be escaped like this \\$.
Remember calling nextDouble may leave you with an empty line, as described here.

You may be able to match a regular expression which takes everything after the $ sign.

And the String#split suggestion will probably work, but you might have to change the delimiter.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ignoring 1 column and reading the other using a Scanner object
 
Similar Threads
How to loop back after an exception is caught?
Get pathname to data file
Method Creation and debugging.
Groovy : Process CSV to SQL
Problem with JTable