Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Checking if a char variable is octal

 
Greenhorn
Posts: 17
Netbeans IDE Windows Vista Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Windows Vista Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Windows Vista Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Windows Vista Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x <= '7' && x >= '0'

;)
 
Campbell Ritchie
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A lot better, isn’t it
 
lowercase baba
Posts: 13063
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic