• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parsing standard input

 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • 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!
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Karthikeyan Rajendraprasad
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic