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 having an string need to take out the integer! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "having an string need to take out the integer!" Watch "having an string need to take out the integer!" New topic
Author

having an string need to take out the integer!

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Hi,
I have a string for example"saharx@yahoo.com" where x is a number between0 to 30. I need to know how can I take out x in INT data type?

1)what I was thinking was to put the string inside of an array, then I have a char array of my string and can have access to the numbers using their indexs. but here I stopped! I dont know how to convert a char array to an integer!

2)please let me know if you know any other way easier than mine?

any suggestion would be appreciate,
thanks,
Sahar.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Check out java.util.regex.Pattern in combination with java.util.regex.Matcher.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And if Rob's regular expression classes are above your head, there are a lot of wonderful methods in the java.lang.String class which can help you extract substrings, find out where "@" is... Once correctly extracted, the java.lang.Integer class will do the rest.


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

The extraction is a bit harder than that. First you need to find the @. Then you would need to go back to find the first character of the number. Using a simple for-loop combined with String.charAt and Character.isDigit will help you out there. If you then have the index of the first character create a substring for the number, then use Integer as Christophe suggested.
Gopinath Karyadath
Ranch Hand

Joined: Oct 14, 2009
Posts: 87
hi
try to split the string int two ( sahar30@yahoo.com )

array[0] = sahar30
array[1] = yahoo.com

int val = Integer.parseInt(array[0].substring(s.length()-2) // return val = 30

Regrads
Gopinath
gopi@c2info.com





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
I still prefer Rob's suggestion about regular expressions. The Java™ Tutorials have a nice section about regular expressions.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Gopinath Karyadath wrote:hi
try to split the string int two ( sahar30@yahoo.com )

array[0] = sahar30
array[1] = yahoo.com

int val = Integer.parseInt(array[0].substring(s.length()-2) // return val = 30

And then the email address becomes sahar4@yahoo.com. You try to parse r4 into an int and you get a NumberFormatException. Like I said, if you don't use regular expressions then you really must traverse the String backwards until you find the first digit.
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
thanks for replies,
I have tried to read java.util.regex.Pattern and java.util.regex.Matcher but I did not understand, its complicated for me.
I tried to follow what you said and this comes out:



but I have 2 error
1) in line 10 char can not be dereferenced (I read it happens when we dont set up the pontee rightly, but I did put a value in it. Am I wrong some where?)
2)line 15 and 19 can not find symbol Integer!!(I have already import "import java.lang.Integer;") what is wrong here?

thanks for your help,
Sahar.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

sahar sa wrote:1) in line 10 char can not be dereferenced (I read it happens when we dont set up the pontee rightly, but I did put a value in it. Am I wrong some where?)

"a" is a char, and char is a primitive type. You don't need to use equals (in fact you can't, as you've seen), but you can use simple == instead:
Note the single quotes around the @. That's how you write down a char. With double quotes it is a String and that's a multi-character object, even if it only has one character.

2)line 15 and 19 can not find symbol Integer!!(I have already import "import java.lang.Integer;") what is wrong here?

Take a better look at Gopinath Karyadath's post. You will see you have missed a very important part of the String-to-int code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41180
    
  45
I have already import "import java.lang.Integer;"

Note that classes in the java.lang package do not need to be imported - the compiler does that automatically.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Did you read the Java Tutorials link I posted yesterday? It is much easier to understand.
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
Thank you All !!! I have it now.
just for other beginners I copy the right code:

thanks again,
Sahar.
Alex Parvan
Ranch Hand

Joined: Dec 10, 2009
Posts: 115
You can make your code a bit simpler :P Java provides methods that make things easier, like sName.toCharArray() or charArray.length. Here is your code a bit shorter. Use OOP at it's full potential :P



"Quoting yourself is stupid" - Me
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Actually, toCharArray() creates a new char[] each time it is called. While that is not worse than the original code, it can be removed completely since it isn't used at all. So the original code becomes this:
That said, the entire loop can be replaced by a simple call to String.indexOf
Alex Parvan
Ranch Hand

Joined: Dec 10, 2009
Posts: 115
That never crossed my mind lol, thanks.
 
jQuery in Action, 2nd edition
 
subject: having an string need to take out the integer!
 
Similar Threads
SOAP Array Response In Web Service Using Java.
Converting Char array to String
Counting uppercase? Part 2!
Doubt on page 547 HFSJ
Question about Arrays