File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion 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: 40028
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ignoring 1 column and reading the other using a Scanner object