• Post Reply Bookmark Topic Watch Topic
  • New Topic

Faulty example code? (Java 7 for Dummies)  RSS feed

 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am currently working myself through Head First Java and Java for Dummies.
Now I am a bit stuck with some example code from Java for Dummies.

I am in Chapter 10 and working with these class-files (and a txt-file, but that's not the problem)

Class Player:



Class PlayerPlus


Class ShowTeamFrame


Class TeamFrame


When I run the code, I get the following error messages:


Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at TeamFrame.<init>(TeamFrame.java:19)
at ShowTeamFrame.main(ShowTeamFrame.java:4)


I have played around a bit but to no avail, what is wrong with the code?

Hints in the right direction are better than straight answers ;-)

Thanks in advance.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow...first post, and you get the code tags right, you posted the full error message, and you're not asking for the solution, just a hint...if any post deserves a cow (<--click that), that does.
 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred *blush*.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus Rauchfuss wrote:
When I run the code, I get the following error messages:


Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at TeamFrame.<init>(TeamFrame.java:19)

at ShowTeamFrame.main(ShowTeamFrame.java:4)


I have played around a bit but to no avail, what is wrong with the code?


The failure is happening ... from the stack trace ... line 19 of the TeamFrame.java file, in the constructor of the TeamFrame class, is a call to the nextDouble() method, which gets a token, but it is not a valid double number.

I think it may be a good idea to just get the raw token -- and print out exactly what is being treated as a double at that position.

Henry
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the Hankees.txt file.
When you craete a TeamFrame object, on line 18 it reads in the first line (using keyboard.nextLine()) and then tries to read a double value (using keyboard.nextDouble()) from the next line, but the next line does not contain a double value (or at least doesn't start with a double value).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion is to look at the data file closely. The stack trace is saying there is a InputMismatchExceptition. According to the API:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

it tells you the line where the issue was found:

at TeamFrame.<init>(TeamFrame.java:19)


if you look at that line:

you have to ask yourself if you are really reading in a double.

be careful...newlines, non-printing characters, heck..non-digits could mess you up. I'm not sure, but if you use European style (1.234,87) and java is configured to look for American(?) style (1,234.87), that may also mess you up.

Can you give us the content of your file?
 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, you are lovely.
You were all correct. The problem was in the data in the text file. I am using a German Java For Dummies and decimals are separated by a "," in German, but my computer and Eclipse is configured for English and thus expects a "."

*facepalm*


But: Problem solved, everything is running smoothly.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and realize that all three of us were probably typing our replies at the same time...Henry just types faster than Joanne, who types faster than me.

We're not trying to gang up on you - we each just didn't see the previous responses before entering our own. Although seven minutes (between Henry and me) is pretty bad...I'm often within a minute. It's sort of comical how some of us compete to be the first to reply.

;-)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Joanne, who types faster than me.

I just type short answers

fred rosenberger wrote:Although seven minutes (between Henry and me) is pretty bad

Seems your maths is as bad as your typing
 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries, I do not feel ganged-up upon. I am a board admin myself and have seen it before
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
fred rosenberger wrote:Although seven minutes (between Henry and me) is pretty bad

Seems your maths is as bad as your typing

sigh...that's what I get for waking up at 4:30 a.m. and going running, instead of sleeping in until 6a.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!