This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Beginning classes and objects question 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 "Beginning classes and objects question" Watch "Beginning classes and objects question" New topic
Author

Beginning classes and objects question

Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
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.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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.
Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
AH! I completely forgot about that! Thank you! Any idea about the calcAge method?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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.
Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
Why are you using a String for purchase date?
Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
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.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
[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.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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.
Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36


Bold is line 122
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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.
Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginning classes and objects question
 
Similar Threads
Joining tables
why it is inserting the values in the rows again
Having issues with classes and objects
Bind class to HttpSession?
Unable to persist the data in hibernate