wood burning stoves*
The moose likes Beginning Java and the fly likes Integer too large? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer too large?" Watch "Integer too large?" New topic
Author

Integer too large?

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I am currently getting the following error message:

C:\java\PoliceDatabase>javac TestApp.java
.\InvalidRecord.java:70: integer number too large: 0181
if ( (newDate < 0181) && (newDate >1220) )
^
1 error

Could someone explain to me why 0181 is to large?
The range as I understand it for an integer is:

-2147,483648 to 2,147483,647

which surely means 0181 is fine?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Integer constants that start with "0" are interpreted as octal in Java. There is no "8" in an octal number.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi,

My guess is that it's because 0181 is an octal literal, an 8 isn't a vaild digit in an octal number.

In Java, a leading 0x indicates that a hexadecimal literal follows, and a leading 0 (zero) indicates that an octal literal follows.

Try just 181 without the 0.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Try just 181 without the 0.


I can't simply put 181 as 0181 is a date?
Does anyone have any suggestions? I am using it to validate data. i.e. a date range where it is stated that it is four digist (MMYY) date range = 0181 to 1220.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Try using actual Date objects rather than ints. The way you are doing it December of 77 is going to be higher than Jan of 05 while December of 05 will be higher than Jan of 77.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Many thanks for your responses so far.

I have now attempted to use dates instead of integers.

I have the following code:

In my VehicleRecord class I have the following method:


In my InvalidRecord class I have the following code:



The error message I am getting when compiling InvalidRecord is the following:

C:\java\PoliceDatabase>javac InvalidRecord.java
InvalidRecord.java:82: operator < cannot be applied to java.util.Date,java.util.
Date
if ( (inputDate < dateFrom) !! (inputDate > dateUntil) )
^
InvalidRecord.java:82: operator > cannot be applied to java.util.Date,java.util.
Date
if ( (inputDate < dateFrom) !! (inputDate > dateUntil) )
^
2 errors

When I look at other peoples code using these operators I can't see why I am getting these errors when it has been suggested I use these operators - they obviously did it successfully so I am confused to why I haven't?
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hello Maureen,
I don't think you can compare dates using < or > operator. Look into after(Date d) or before(Date d) method in java.util.Date. It returns a boolean and its a preferred way to do comparison. If there's anything you don't understand about those methods, feel free to post back and i'll try my best to explain them.
P.S: You might also want to look at this line of the code:



Unless it was a typo, it should be || not !!
[ April 08, 2005: Message edited by: Mala Sharma ]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Mala

Many thanks - I'll go and check it out.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have now changed my InvalidRecord class to the following:


My VehicleRecord class remains the same as above.

I have now getting the following error message when compiling?

C:\java\PoliceDatabase>javac InvalidRecord.java
InvalidRecord.java:76: unreported exception java.text.ParseException; must be ca
ught or declared to be thrown
Date inputDate = sdf.parse (date);
^
InvalidRecord.java:77: unreported exception java.text.ParseException; must be ca
ught or declared to be thrown
Date dateFrom = sdf.parse (from);
^
InvalidRecord.java:78: unreported exception java.text.ParseException; must be ca
ught or declared to be thrown
Date dateUntil = sdf.parse(until);
^
3 errors

C:\java\PoliceDatabase>

Is this to do with the parse or is my date format incorrect? Any suggestions are appreciated!
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
these errors are to be expected, and are actually good. think about it: when you use this code -

you're asking the "sdf" object to take the string date and try to make sense of it, and then return the date it describes, in the form of a Date object.

but what if the string date doesn't make sense? if, for example, the user entered some gibberish? well, then, "sdf" will raise an exception, and your code will have to deal with that exception somehow. so, you'll need a try/catch pair for that, and the error you're getting is telling you which exception in particular "sdf" might throw.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Many thanks for your response.

I glanced at this. I felt I needed a catch ParseException e?

I'm I thinking right? Is this a checked or unchecked exception - I looked in the API and couldn't find it
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
you're quite correct that ParseException is the one. and yes, it's a checked exception - it must be; they're the only kind that give you compile-time errors like this one.

that said, i've had trouble finding exceptions in the API as well. Java's exception hierarchy is a mess, to be honest - there's an awful lot of them, defined all over the various parts of the API, and too few by far are general-purpose ones you can reuse for your own needs. it seems i'm always defining a new exception of my own, and usually after fifteen minutes of frustrating digging through the API trying to find one i could just reuse!
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
M Beck,

Many thanks for your response. Much appreciated!

As this subject is now more about the try and catch exception errors and not big integer I have opened a new thread called try and catch - I know when I look in some of the history threads it would be more meaningful to me so I am attempting to help other people too.

Please don't be shy in responding to the try and catch thread - I'm pulling my hair out here!
 
jQuery in Action, 2nd edition
 
subject: Integer too large?
 
Similar Threads
what type is the array.length variable ??
Using an int to hold a four digit date
Literal values doubt
java.util.Date setTime() API gives error
if test with or