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

value of a character  RSS feed

 
Tina Long
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string that is composed of numbers and letters. I want to get all the numbers out of the string.

I have tried:



but the words "HERE" is never printed. How do I tell what the value of aChar is? I really want to know if it's a number or not - but I figured I'd start with telling if it was the number 2 or not then go on to figure out how to tell if the character is a number...however I'm doing something wrong because "HERE" never prints - so it's not getting into my IF statement.

Any thoughts?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change...

System.out.println(aChar);

...to...

System.out.println(aChar + " = " + (int)aChar);

...and see what the int values of these chars actually are (noting that none of these == 2).

Consult this table to see where these values come from:
http://www.lookuptables.com/

Then consider the difference between aChar == 2 and aChar == '2'.
[ June 15, 2005: Message edited by: marc weber ]
 
Tina Long
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc - that makes sense now. I didn't realize I could use single quotes in my compare statement. I kept using double quotes and Java didn't like that. So I settled for no quotes - but now I realize that was completely wrong...ugh...

Given the table that you referenced - I found that numbers ascii values are between 48 and 57. So I changed my code to find the ascii values of the number and it works for what I need...but is it really a good solution?

I changed my for loop as below:



It does work - it gets the number out of the string - but is this really the best way to do this?
 
Tina Long
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally figured out another option - but I don't really understand how it works - I've read about it but I just don't totally understand it. This is probably a better solution though instead of using the ascii value...but I'm not for sure...any thoughts?



Thanks to those who have helped me!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is a much more elegant solution!

Basically, the isDigit method in Character returns true if the Unicode value of the char is within the ranges of various number representations:
  • Unicode values are hexadecimal, so these are under the "Hx" column in the ASCII table reference. Thus, the characters 0-9 are represented by decimal values 48-57 or Unicode (hex) values 30-39 (i.e., '\u0030' through '\u0039').
  • 0-9 are "ISO-LATIN-1 digits," which is one of the number representations recognized by the isDigit method. (The API lists others, but for standard keyboard values, 0-9 is really all you need to be concerned with.

  • [ June 16, 2005: Message edited by: marc weber ]
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Tina Long:
    ... I didn't realize I could use single quotes in my compare statement. I kept using double quotes and Java didn't like that. So I settled for no quotes...

    In this context...
  • Single quotes denote type char.
  • Double quotes denote type String.
  • No quotes imply a value (type int).
  •  
    Joel McNary
    Bartender
    Posts: 1840
    Eclipse IDE Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    if ((int)aChar >= 48 && (int)aChar <= 57) {


    Although I would probably use the isDigit() method for best readibility, if you were to use this idiom the readability can be improved as such:



    Also note that the cast to an int is not necessary.
     
    Tina Long
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you for your explanations...

    I am going to go with the Character.isDigit - I think it's the best solution.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Tina Long:
    ... I am going to go with the Character.isDigit - I think it's the best solution.

    That's why it's there. The API is your friend.

    I'm also glad you found StringBuffer -- a very helpful class, much more efficient than concatenating (creating new) String objects.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!