| Author |
parsing standard input
|
Greg Schwartz
Ranch Hand
Joined: May 11, 2003
Posts: 132
|
|
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
Joined: Apr 24, 2002
Posts: 137
|
|
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:
|
use the [CODE] tags - it makes it much easier for people to help you.
|
 |
Greg Schwartz
Ranch Hand
Joined: May 11, 2003
Posts: 132
|
|
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
Joined: Apr 16, 2003
Posts: 70
|
|
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.
|
Karthikeyan<br />SCJP 1.4, SCWCD.
|
 |
 |
|
|
subject: parsing standard input
|
|
|