This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to fetch only numbers from string

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • 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: 20492
54
Chrome Eclipse IDE Java Windows
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer using regex:
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Zandis Murāns .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic