Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking if a char variable is octal

 
Angel Kal
Greenhorn
Posts: 17
Chrome Netbeans IDE Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Netbeans IDE Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Netbeans IDE Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can delete about ⅔ of that code and it will still work. Let’s see the shortened version!
 
Angel Kal
Greenhorn
Posts: 17
Chrome Netbeans IDE Windows Vista
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x <= '7' && x >= '0'

;)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot better, isn’t it
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic