This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to fetch only numbers from string

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


then how to bring out only numbers from that string .
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Use String.split, then try to parse each element, catching the NumberFormatExceptions for non-numbers.
2) Use java.util.regex.Pattern with the right regular expression.
3) Use java.util.Scanner and its hasNext(), next(), hasNextInt() and nextInt() methods.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer using regex:
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zandis, although your solution was quite good, I would like to ask you to LetThemDoTheirOwnHomework. In other words, don't provide full answers where a hint will help the towards the topic starter to find the answer themselves.
 
shyam sunder prasad
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Zandis Murāns .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic