wood burning stoves*
The moose likes Java in General and the fly likes How to fetch only numbers from string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to fetch only numbers from string" Watch "How to fetch only numbers from string" New topic
Author

How to fetch only numbers from string

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62


then how to bring out only numbers from that string .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

I prefer using regex:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: Mar 23, 2011
Posts: 62
thanks Zandis Murāns .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to fetch only numbers from string
 
Similar Threads
Selecting characters from a string
Extracting numbers from string
How to devide a task into several parts
object creation in GC
Lost, need help