This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 837
    
  14

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: 18501
    
  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: 7492
    
  18

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: 37902
    
  22
Another way is to use a Scanner.You wll suffer an InputMismatchException if your String does not start with a valid int.
 
Don't get me started about those stupid light bulbs.
 
subject: i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int.
 
Similar Threads
convert a string having decimal point to double or long
Making a binary to decimal converter
Python Calculator: Several options, changing the number
Adding a new array element is not saving to memory
very basic java