Home arrow static arrow Java Programming [Archive] - Conversions of characters
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Conversions of characters
This topic has 7 replies on 1 page.

Posts:12
Registered: 8/7/04
Conversions of characters  
Aug 8, 2004 9:33 AM



 
Hi,

I want to read a string and store the string part separately from the numbers. For example,
Ex12345. I want to read this line and separate the Ex from the 12345. I thought of converting each character into ASCII, and if its value fits within the range of letters, then I can separate it. However, I don't know how to do that in Java. If anyone can please suggest how to go about doing this, or another way of separating, please help out. Thanks.
 

Posts:18,384
Registered: 21.03.00
Re: Conversions of characters  
Aug 8, 2004 10:15 AM (reply 1 of 7)



 
Hi,

Will the Strings always begin the letters followed by digits? Can they be mixed etc.

/Kaj
 

Posts:262
Registered: 10/8/03
Re: Conversions of characters  
Aug 8, 2004 10:22 AM (reply 2 of 7)



 
You mean like that?
String str="EX12345";        int i=0;for(; i<str.length(); i++) {    char c=str.charAt(i);    if(c>='0' && c<='9') break;}        String chars=str.substring(0,i);String number=str.substring(i);        System.out.println(chars);System.out.println(number); 
 

Posts:12
Registered: 8/7/04
Re: Conversions of characters  
Aug 8, 2004 1:07 PM (reply 3 of 7)



 
Hey, Zoidberg42, thanks for your help! It works perfectly.
I just want to know, how come in the for loop, we just say for (;.....), and not for (int i=0;......)? Because I noticed that it refused to break if I wrote it the latter way. Otherwise, thank you very much!
 

Posts:18,384
Registered: 21.03.00
Re: Conversions of characters  
Aug 8, 2004 1:14 PM (reply 4 of 7)



 
Hi,

I just wanted to add a comment. You can rewrite the loop to:

int i=0;for(; !Character.isDigit(str.charAt(i)); i++);


/Kaj
 

Posts:12
Registered: 8/7/04
Re: Conversions of characters  
Aug 8, 2004 2:17 PM (reply 5 of 7)



 
Thanks. Again, I'm wondering how come we write:
for(; !Character.isDigit(str.charAt(i)); i++);
and not for (int i=0; .....)? What does just (;) do?
And is Character.isDigit a way of simply checking if a character is a digit?
 

Posts:18,384
Registered: 21.03.00
Re: Conversions of characters  
Aug 8, 2004 2:24 PM (reply 6 of 7)



 
Hi,

Thanks. Again, I'm wondering how come we write:
for(; !Character.isDigit(str.charAt(i)); i++);
and not for (int i=0; .....)?

We later on need the value of i, so we can't declare it inside (), because then it won't be in scope when we need the value for the substring parts.

And is Character.isDigit a way of simply checking if a
character is a digit?
Yes it is

/Kaj
 

Posts:12
Registered: 8/7/04
Re: Conversions of characters  
Aug 8, 2004 2:41 PM (reply 7 of 7)



 
Thanks so much!! My assignment is finally working and I thank you very much! It's not the most efficient program, but at least it does the job. Thanks for your help and time! :-)
 
This topic has 7 replies on 1 page.