This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 3502
    
  13
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: 19684
    
  20

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 - OCEEJBD 6
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: 1776
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: 1776

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: 1776
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: 1776
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.
Abdoul Kader Soumahoro
Greenhorn

Joined: Aug 28, 2013
Posts: 22
hi check this out! very simple and does the job

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
And when your input is "173.vsd9"?
Abdoul Kader Soumahoro
Greenhorn

Joined: Aug 28, 2013
Posts: 22
it shoudn't matter if I am only looking for integers. But at the same I see your point because that would not work if i am trying to get the integer values from a filename. in that case I would use the following:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
That loop is getting more complicated as I watch. There are more elegant ways to choose the leading number, even with a loop.
Abdoul Kader Soumahoro
Greenhorn

Joined: Aug 28, 2013
Posts: 22
Can I please know what is your suggestion to improve this code
Gary Fletcher
Ranch Hand

Joined: Nov 12, 2013
Posts: 36
Regex..they work very well.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Don't use \n, least of all after printf, where you should use %n
Don't use += for Strings, least of all when you are putting a String literal together. UsePreferably without the cat on the keyboard effect!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

Shikha Upadhyaya wrote:Below is the code:...

Shikha,

Please DontWriteLongLines (←click).

It makes your thread very hard to read, and it's actually bad coding practice.
Unfortunately, there are now so many in this thread that it's probably too late.

Also - @Gary, Abdoul - Please don't quote excessively long code lines - or, if you do, break them up -
for all the reasons mentioned in the link.

Thanks.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 533
    
    8
Winston Gutkowski wrote:
Shikha Upadhyaya wrote:Below is the code:...

Shikha,

hope the OP is still around after two years...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

Piet Souris wrote:hope the OP is still around after two years...

Doh-h-h!

Maybe Gary and Abdoul are though.

Winston
 
Consider Paul's rocket mass heater.
 
subject: Extracting integers from a string.