This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer too large?

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20893
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer constants that start with "0" are interpreted as octal in Java. There is no "8" in an octal number.

Henry
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mala

Many thanks - I'll go and check it out.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic