wood burning stoves*
The moose likes Beginning Java and the fly likes what's wrong with the code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what Watch "what New topic
Author

what's wrong with the code?

Marvin Domingo
Greenhorn

Joined: Dec 17, 2007
Posts: 20
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
*/



[CENTER]I wanna download the internet[/CENTER]<br />[IMG]http://www.w3schools.com/images/downloadwww.gif[/IMG]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Which is line 50 and which is line 71 ?


Joanne
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

The end method uses a ), not a }


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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

Joined: Dec 20, 2007
Posts: 1
Thank you !!I am thinking about it
Marvin Domingo
Greenhorn

Joined: Dec 17, 2007
Posts: 20
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

Joined: Mar 06, 2008
Posts: 3
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?



There are 10 types of people in the World; those that understand binary and those that don't.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
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.


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: what's wrong with the code?
 
Similar Threads
GuessGame errors
Exceptation in thread "main" java.lang.NoSuchMethodError: main
Head First Java Book, Chapter 2
Please Help with Guessing Game (Head First Java)
Very new to java