• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

having an string need to take out the integer!

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out java.util.regex.Pattern in combination with java.util.regex.Matcher.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That never crossed my mind lol, thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic