Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to do octal number comparision

 
swatione chowdary
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this following code:

String number= "2042775656";
int num=Integer.parseInt(number);
System.out.println(num);

if(num >= 0000008704 && num <= 0000008999 ){
//some code....
}



it is giving the folloing error:

Octal 0000008704 (digit 8) is out of range .




then how can i compare

int num variable weather it falls between these numbers
0000008704 � 0000008999
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to compare octal numbers ? But the octal system contains digits from 0 to 7. So 0000008704 is not an octal number.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java (just like in C and C++), if you start an integer literal with a 0 (like you are doing with 0000008704 and 0000008999 in your code), it is interpreted as a number in the octal numeral system instead of as a decimal number.

You didn't mean to use octal numbers, so you should just remove the zeroes.
 
swatione chowdary
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no not like that when i executed that code it is giving that error

my ultimate goal is to compare the number weather it falls between this range.

0000008704�0000008999
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to repeat myself.
Neither 8704 nor 8999 are octal numbers.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"no not like that when i executed that code it is giving that error"

No, you got that error while trying to compile it. It doesn't compile, so there's no way you can be executing this code.

Tell me: What is the difference between the number 0000008704 and the number 8704?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper is right... why do you need all those leading 0's? or why don't you use 0000000000000000000000000000000000000000000000000000008704 and 0000000000000000000000000000000000000000000000000000008999?

because you don't need leading 0's. Therefore, when you put one on your number, Java assumes you are writing a number in base-8. The only allowed digits in base-8 are 0,1,2,3,4,5,6, and 7. You're numbers have the digit '8', so Java is confused, and refuses to compile your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic