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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Liutauras Vilda
Sheriffs:
• Paul Clapham
• Jeanne Boyarsky
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Piet Souris
• Carey Brown
Bartenders:
• Jesse Duncan
• Frits Walraven
• Mikalai Zaikin

Integer too large?

Ranch Hand
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:
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?

author
Posts: 23923
142
• Number of slices to send:
Optional 'thank-you' note:
Integer constants that start with "0" are interpreted as octal in Java. There is no "8" in an octal number.

Henry

Ranch Hand
Posts: 221
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:

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.

Ranch Hand
Posts: 1071
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:
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?

Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:
Mala

Many thanks - I'll go and check it out.

Maureen Charlton
Ranch Hand
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:
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!

Ranch Hand
Posts: 323
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 323
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:
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!

 pie. tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton