File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes i need to grab a Sting like this: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int." Watch "i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int." New topic
Author

i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int.

Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int. just the numbers like for the first i need an int = 1 25 or similar; and for the second int = 3;

I've tried doing:

Integer foo = Integer.parseInt("3 selected");

but of course its not working

Can someone advise thanks!!


I know is only java
BUT I LIKE IT!!!
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1048
    
  17

This prints 125. But is it what you want? Do you want just to remove all non-number characers or do you want to get 1 and 25 separately from that String?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Marcelo Bordolli wrote:i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int. just the numbers like for the first i need an int = 1 25 or similar; and for the second int = 3;

I've tried doing:

Integer foo = Integer.parseInt("3 selected");

but of course its not working

Can someone advise thanks!!



Basically, the easiest way to find numbers in a string is to use regular expressions... see the java.util.regex.Pattern class. Unfortunately, if you don't know regular expressions, it is probably not a good idea to use it -- although, I do highly recommend that you learn it.

Without regular expression, you can loop through the string looking for the characters (that are digits), and then use the substring() method to extract the numbers to be parsed into a int.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

Thanks man that would do!!
Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

Thank you!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Marcelo Bordolli wrote:Thanks man that would do!!

Do be careful though. Remember: "-25" is a perfectly valid number.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Another way is to use a Scanner.You wll suffer an InputMismatchException if your String does not start with a valid int.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int.