Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

having an string need to take out the integer!

 
sahar sa
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.regex.Pattern in combination with java.util.regex.Matcher.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still prefer Rob's suggestion about regular expressions. The Java™ Tutorials have a nice section about regular expressions.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the Java Tutorials link I posted yesterday? It is much easier to understand.
 
sahar sa
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That never crossed my mind lol, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic