File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes parsing standard input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parsing standard input" Watch "parsing standard input" New topic

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.
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,
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
Originally posted by Jon Dornback:

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.
I agree. Here's the link:
subject: parsing standard input
It's not a secret anymore!