File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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: 19684
    
  20

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: 19684
    
  20

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 .
 
jQuery in Action, 2nd edition
 
subject: How to fetch only numbers from string