This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Extracting integers from a string. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Extracting integers from a string." Watch "Extracting integers from a string." New topic
Author

Extracting integers from a string.

Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

I have a string as
How do I extract 98 and 100 and store them in an array. I wrote the following code:

It throws a NumberFormatException. What do I do?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Shikha Upadhyaya wrote:It throws a NumberFormatException. What do I do?

Catch the exception and ignore it. Only increment i when no exception is thrown.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

StringTokenizer isn't a proper solution here. A Pattern / Matcher pair or a Scanner (where you can test for hasNextInt) are much better.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

Rob Spoor wrote:StringTokenizer isn't a proper solution here. A Pattern / Matcher pair or a Scanner (where you can test for hasNextInt) are much better.

I tried hasNextInt also but I'm not able to make out where my code went wrong. So, thought of trying this out. It is also not working.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1778
Shikha Upadhyaya wrote:
Rob Spoor wrote:StringTokenizer isn't a proper solution here. A Pattern / Matcher pair or a Scanner (where you can test for hasNextInt) are much better.

I tried hasNextInt also but I'm not able to make out where my code went wrong. So, thought of trying this out. It is also not working.

Can you please post the code you tried with hasNextInt()?
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

John Jai wrote:Can you please post the code you tried with hasNextInt()?

Below is the code:

marks.txt:
John Jai
Bartender

Joined: May 31, 2011
Posts: 1778

You are having a scanner that scans the lines (lineScanner). But are you making use of that in your code?
The fileScanner shall be used only to read the file line by line. All operations (inner while loop, and the if and else inside it) you happen to do on the line do it on the line scanner.

There are couple more things I spotted but correct this one first.
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

John Jai wrote:
You are having a scanner that scans the lines (lineScanner). But are you making use of that in your code?
The fileScanner shall be used only to read the file line by line. All operations (inner while loop, and the if and else inside it) you happen to do on the line do it on the line scanner.

There are couple more things I spotted but correct this one first.


Got it!!! Thank you in helping me spot my mistakes. The corrected code snippet follows:
John Jai
Bartender

Joined: May 31, 2011
Posts: 1778
Welcome... two things...
1. You might need to set count to 0 before you start parsing each line.
2. Did you check whether 100, came as a integer token? Or it was ignored since it had a trailing comma?

Use System.out.println() to check which tokens were ignored...
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

John Jai wrote:Welcome... two things...
2. Did you check whether 100, came as a integer token? Or it was ignored since it had a trailing comma?

Use System.out.println() to check which tokens were ignored...

Oh yeah! 100 is not printed! I had given a space between 100 and , in the code earlier. What has to be done if 100 should be retrieved when it is 100, and not 100 , ?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1778
One option is adding , as a delimiter along with the white space character to the scanner. Try adding the second line in the below code for the line scanner.
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

Ok.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Extracting integers from a string.
 
Similar Threads
Tokenizing string in Java
problem reading a string from a text file
Reversing a string
Regarding String Tokenizer
retrieving value from a text box