Forums Register Login

Beginning classes and objects question

+Pie Number of slices to send: Send
Hi all. This is the code I have so far.



The calcAge method does not work and I get this error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at AssetDriver.main(AssetDriver.java:37)

I am trying to have the calcAge method take the mm/dd/yyy string, convert it to an int, isolate JUST the yyyy, and then subtract the current year from what the user enters to get the difference.

I have tried everything I can think of. If anyone has any suggestions please help. Also here is my driver class I am testing it with:



In addition to the calcAge error; my driver class keeps combining What was the purchase date and what is the employee ID into one question instead of two.

+Pie Number of slices to send: Send
The Scanner behavior often trips people up. After calling nextInt() or nextDouble(), you should immediately call nextLine() to clear the buffer and get ready for the next input.
+Pie Number of slices to send: Send
AH! I completely forgot about that! Thank you! Any idea about the calcAge method?
+Pie Number of slices to send: Send
See if purchaseDate in calcAge() is what you expect. Use substring(6) not 7, then check stringYear is right. Just basic stuff like that and you'll get it.
+Pie Number of slices to send: Send
I have tried all of that, still can't seem to get it working. Classes and objects are entirely new to me so I'm sure I"m doing something wrong that is an easy fix that I just can't see.
+Pie Number of slices to send: Send
Why are you using a String for purchase date?
+Pie Number of slices to send: Send
It is a requirement of the assignment + I don't think anything but a string would accept the / character since purchaseDate HAS to be in a mm/dd/yyyy format as well; another requirement of the assignment. Anyhow I figured out my problem; when I was entering the data I was typing an int + a string for the "what is the age of your item"; which is an int variable. Then I got another error and figured out because the "need a replacement" question is a boolean the answer has to be in true/false not yes or no. Which led to this error in the calcAge method statement:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at Asset.calcAge(Asset.java:122)
at Asset.printData(Asset.java:159)
at AssetDriver.main(AssetDriver.java:52)

I have tried many different string indexes. 6 should have worked.

+Pie Number of slices to send: Send
That problem goes to show how bad Strings are for that sort of thing. If you had used a Date object, or a Calendar object, it would have been much easier.

I think you should go and discuss use of Strings there with whoever set the assessment.
+Pie Number of slices to send: Send
In the code you've posted, there's no line 122, and no way to get a -1 index. Can you post calcAge() the way it is now, and tell us which line the exception comes from?
+Pie Number of slices to send: Send
 

Ryan Hickman wrote:Hi all. This is the code I have so far...


Ryan,

Please DontWriteLongLines (←click). I've removed yours this time, but it makes your threads (and code) hard to read.

Thanks

Winston

PS: I also get a bit worried when I see a constructor (or method) with 8 parameters, especially when they are clearly not all needed. You might want to look at the Bulider Pattern for handling this.
+Pie Number of slices to send: Send
[Edit] Ryan: I've removed your code paste for the reasons I said in my previous post.
PLEASE DON'T POST CODE WITH LONG LINES.

<the remainder of your post>

Error:

Exception in thread "main" Here is the data that you provided:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at Asset.calcAge(Asset.java:122)
at Asset.printData(Asset.java:159)
at AssetDriver.main(AssetDriver.java:52)

The first code is the main class the second code is the driver class testing the main class. I wanted to post more than just the calcAge method because the constructors may have something to do with the errors.
+Pie Number of slices to send: Send
Unless calcAge() refers to fields set by the the constructor, then, no, the constructor has nothing to do with the error. Just post the calcAge() code, and remember to tell us which line is line 122 in your source file.
+Pie Number of slices to send: Send


Bold is line 122
+Pie Number of slices to send: Send
OK, you're getting that because purchaseDate is am empty string. You've got to trace that back to the point where you read it from the scanner. Use System.out.println() to find out its value at various points in your program.
+Pie Number of slices to send: Send
Resolved it; had to add another buffer in the driver class for calculating the int. All that trouble for just one buffer statement to fix it all. Thank you everybody for your help.
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1021 times.
Similar Threads
Having issues with classes and objects
Reference data type
Bind class to HttpSession?
Unable to persist the data in hibernate
why it is inserting the values in the rows again
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:52:35.