• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

parsing standard input

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering if someone could recommend a better way to parse integer values from standard input.
Currently, I am using BufferedInputReader that returns the input in the form of a string. I then pass the string to a StringTokenizer. I check each token using Integer.parseInt(tokenValue) . I am then putting the ints into an array for processing. I'm using an array instead of a vector because the Vector class wouldn't process ints and it seemed to be a hassle to convert to the Integer type.
If anyone has a cleaner way of going about doing this, please let me know.
Thanks!
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're using the same algorithm I would use. If you need a re-sizable array, use java.util.ArrayList (don't use Vector - most of what i've read warns against it). To put ints in to the ArrayList, use this in the tokenizing loop:
 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jon -
Thanks so much for your advice! I'm glad to hear that I was pretty close. I didn't know about ArrayList so I'll be sure to use that in my code.
Thanks again,
Greg
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jon Dornback:
[/QB]


one performance tip.
what you do is
1. get the number as String
2. convert it to int
3. convert to Integer just before adding to the Arraylt
4. while getting it back you convert it to int again.
why do you convert this no of times. instead you can check whether it is an integer or not and store the string value directly in the ArrayList and then convert it to int while getting it from the list.
 
reply
    Bookmark Topic Watch Topic
  • New Topic