• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ignoring 1 column and reading the other using a Scanner object

 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, but you could split and get the second column.

Maybe something like this:

nextLine().split(" ")[1];
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic