| Author |
Checking if a char variable is octal
|
Angel Kal
Greenhorn
Joined: Dec 11, 2011
Posts: 17
|
|
Just to be clear this IS NOT homework...
Im trying to prepare for a test that deals with the Character, and StringBuilder(&Buffer) classes. Im working on some of the exercises in the back of the book, and even after throughly reading about the digit, and forDigit methods of the Character class, Im not understanding how they want me to accomplish this....The exercise says...
Assume that x is a char variable has been declared and already given a value.
Write an expression whose value is true if and only if x is an octal (Base 8) digit (0-7).
Ive tried it numerous ways and Im not getting the correct results when I check my expression, then display the boolean in the output window, and would just like to know what anyone else would do at this point!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Angel Kal wrote:Just to be clear this IS NOT homework...
Im trying to prepare for a test that deals with the Character, and StringBuilder(&Buffer) classes. Im working on some of the exercises in the back of the book, and even after throughly reading about the digit, and forDigit methods of the Character class, Im not understanding how they want me to accomplish this....The exercise says...
Assume that x is a char variable has been declared and already given a value.
Write an expression whose value is true if and only if x is an octal (Base 8) digit (0-7).
Ive tried it numerous ways and Im not getting the correct results when I check my expression, then display the boolean in the output window, and would just like to know what anyone else would do at this point!
It would help if you tell us what you tried -- we can't give you a hint if we don't know what you are doing wrong.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
|
A char is simply a number; it isn’t octal or decimal or anything. If you want to check whether a char is in a particular range, you can use arithmetic operators, eg + -, or more usefully > ≥ etc. You can find the values for 0 to 7 here, and that method can be written as a single statement.
|
 |
Angel Kal
Greenhorn
Joined: Dec 11, 2011
Posts: 17
|
|
From what Ive understood I thought that this is what they were looking for
Character.digit(x) >= '0' && Character.digit(x) <= '7'
Im semi new to this so maybe Im just not comprehending the method. I feel like it would be easier if they were asking for several statements and not just AN expression!
|
 |
Angel Kal
Greenhorn
Joined: Dec 11, 2011
Posts: 17
|
|
|
oooo...It worked, thanks for the tips! I didnt even need to use the method, I wasnt even thinking that I could just compare a char variable (which is actually a number) to the values of 0 & 7! wuhu! Thanks again!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
|
You can delete about ⅔ of that code and it will still work. Let’s see the shortened version!
|
 |
Angel Kal
Greenhorn
Joined: Dec 11, 2011
Posts: 17
|
|
x <= '7' && x >= '0'
;)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
A lot better, isn’t it
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
Angel Kal wrote:x <= '7' && x >= '0'
is this guaranteed to work for all encodings? For some reason, i have it in the back of my brain that this will work for ASCII, but there could conceivably be a character set where it does not...
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
|
There is bound to be an encoding which that won’t work for, but most encodings I have tried have their very low ranges the same as ASCII. I shall have a look at Joel Spolsky’s article; they say you should read it at least once a year!
|
 |
 |
|
|
subject: Checking if a char variable is octal
|
|
|