Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting integers from a string.

 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer isn't a proper solution here. A Pattern / Matcher pair or a Scanner (where you can test for hasNextInt) are much better.
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Can you please post the code you tried with hasNextInt()?

Below is the code:

marks.txt:
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi check this out! very simple and does the job

 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when your input is "173.vsd9"?
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I please know what is your suggestion to improve this code
 
Gary Fletcher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regex..they work very well.


 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Piet Souris
Ranch Hand
Posts: 1153
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Shikha Upadhyaya wrote:Below is the code:...

Shikha,

hope the OP is still around after two years...
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hope the OP is still around after two years...

Doh-h-h!

Maybe Gary and Abdoul are though.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic