Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Checking if a char variable is octal

Angel Kal
Greenhorn
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
Marshal
Posts: 21117
78
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

Campbell Ritchie
Sheriff
Posts: 48957
60
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
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
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
Posts: 48957
60
You can delete about ⅔ of that code and it will still work. Let’s see the shortened version!

Angel Kal
Greenhorn
Posts: 17
• 1
x <= '7' && x >= '0'

;)

Campbell Ritchie
Sheriff
Posts: 48957
60
A lot better, isn’t it

fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
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...

Campbell Ritchie
Sheriff
Posts: 48957
60
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!