• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ignoring 1 column and reading the other using a Scanner object

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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];
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic