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.
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?
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 ]
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
Is this to do with the parse or is my date format incorrect? Any suggestions are appreciated!
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 stringdate 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 datedoesn'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.
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!
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!
No holds barred. And no bars holed. Except this tiny ad: