wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to do octal number comparision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to do octal number comparision" Watch "how to do octal number comparision" New topic
Author

how to do octal number comparision

swatione chowdary
Greenhorn

Joined: Apr 07, 2005
Posts: 27
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

You want to compare octal numbers ? But the octal system contains digits from 0 to 7. So 0000008704 is not an octal number.


[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
swatione chowdary
Greenhorn

Joined: Apr 07, 2005
Posts: 27
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Sorry to repeat myself.
Neither 8704 nor 8999 are octal numbers.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

"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

Joined: Oct 02, 2003
Posts: 11351
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
GeeCON Prague 2014
 
subject: how to do octal number comparision