• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what's wrong with the code?

 
Marvin Domingo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started to read the "head first java book" this is the guess game code
I don't want to copy the exact code from the book when running it in my IDE.
I type the code in a single class, but of course when i save or compile it, the other classes should appear in the class folder as a seperate class.
the 2 errors in my version of the code says;

illegal start of expression in line 50
reached end of file while parsing line 71

/*why is that? kindly check my version so that i can configure the mistake? i
*already checked the open and close braces
*/

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is line 50 and which is line 71 ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marvin Domingo:
I type the code in a single class


If you mean by this that all three classes are in a single file, then only one of them can be public. Whichever one you make public, the file must have the same name as that class.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The end method uses a ), not a }
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another issue: unless the random number is 0, this code will loop forever. Why? Because you never pass the guess back to the loop.

So, let the "guess" method return the number:

Then, inside the loop, assign these values to your variables:


There's another bug, with the checking against the target, but you can find that yourself
[ December 19, 2007: Message edited by: Rob Prime ]
 
ren zhitao
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you !!I am thinking about it
 
Marvin Domingo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the errors

first - the "}" end for the method

second - the method should return a number, else it is an infinite loop..lol my IDE crashed last night because of the infinity..

third - the method of each player should be assigned to the boolean variables.

fourth - and yeah..you're right..only one should be public, and it should be named after the name of the file
 
Mark Walton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using this book and the code works perfectly (as typed in the book) without passing the guess back. Is that because I'm using three .java files and 3 .class files?

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Another issue: unless the random number is 0, this code will loop forever. Why? Because you never pass the guess back to the loop.

So, let the "guess" method return the number:

Then, inside the loop, assign these values to your variables:


There's another bug, with the checking against the target, but you can find that yourself

[ December 19, 2007: Message edited by: Rob Prime ]



Even if the random number becomes 0. it will never come out of the loop in the above program. There is nothing to do with random no. value to come out of the loop unless a return statement is added.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a break statement which is called if any of the players has guessed right. In the erroneous code however their guesses were kept at 0, so that is why only random number 0 would lead to the break statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic