• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java Code Error  RSS feed

 
Ranch Hand
Posts: 49
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is what Head First Java calls "Ready Bake Code" - its code that i'm not far enough in the book to understand and i'm basically supposed to copy it.

So I did and here it is:



Here is the output in the command prompt:

C:\JAVA SRC\DotComGame>javac GameHelper.java
GameHelper.java:18: error: <identifier> expected
inputLine = is.readLine();
^
GameHelper.java:19: error: illegal start of type
if (inputLine.length() == 0)
^
GameHelper.java:19: error: = expected
if (inputLine.length() == 0)
^
GameHelper.java:19: error: ')' expected
if (inputLine.length() == 0)
^
GameHelper.java:19: error: '{' expected
if (inputLine.length() == 0)
^
GameHelper.java:19: error: not a statement
if (inputLine.length() == 0)
^
GameHelper.java:19: error: ';' expected
if (inputLine.length() == 0)
^
7 errors


I can't figure out whats wrong with it - any help will be GREATLY appreciated - Thanks!

 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clue: Java is case-sensitive.
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 15 you start a try block like this: "try (" and at line 21 you close the block like this: "}". (The indentation of your code is pretty random, which makes it hard to follow, but as far as I can see that's how it's arranged.)

Look closely at the brackets you use to start and end the block... do you notice anything strange?
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a bunch of typos, in line 13, line 15, line 22, line 23, line 29 (2, actually), line 33, line 68 and line 72.

I think line 64 should read "x = 0;"

Line 81 and 82 seem to be switched around.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I think "Exception " =e should read "Exception " + e.
Please use consistent indentation and spacing. We have some suggestions here. You can confuse yourself badly if your indentation is inconsistent.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is rather old‑fashioned. Try if (inputLine.isEmpty()) instead. The isEmpty method was added after HFJ was printed.
 
Franklin Marquette
Ranch Hand
Posts: 49
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks - I was really tired last night and did not notice all those errors - thanks for putting up with me
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!