Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Extracting integers from a string.

 
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?
 
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.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi check this out! very simple and does the job

 
Marshal
Posts: 69806
277
  • 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
Marshal
Posts: 69806
277
  • 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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regex..they work very well.


 
Campbell Ritchie
Marshal
Posts: 69806
277
  • 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!
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
Bartender
Posts: 4001
156
  • 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
Posts: 10777
71
Hibernate Eclipse IDE 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
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic