File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginning classes and objects question

 
Ryan Hickman
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AH! I completely forgot about that! Thank you! Any idea about the calcAge method?
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a String for purchase date?
 
Ryan Hickman
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ryan Hickman
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Bold is line 122
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic