• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java 2nd edition - Question about exercise (Guessing Game)  RSS feed

 
Dustin Douglas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, happy new year everyone!

I'm just starting to learn more about Java and I'm stuck on this little exercise from the Head First Java book. Here is the code snippet provided by the book and the error:

When I attempt to compile this, I get 2 errors.

Error: Not a statement
number - (int) (Math.random() * 10)

Error: ';' expected
number - (int) (Math.random() * 10)

The code in question appears like this:
int targetNumber = (int) (Math.random() * 10);

I see a semicolon, and I don't understand the "not a statement" problem. I retype the code, and the code around it, and the whole program - errors still present. Then I opened the digital version of the book and copied the entire code into notepad++ and I still can't compile the code due to these 2 errors.

I must be missing something. Any clues? I know the book was designed for Java 5, and I downloaded the newest release (jdk 8). I don't think it's a version issue, but hell, it could be. I'm open to suggestions!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dustin Douglas wrote:First off, happy new year everyone!

Same to you, and welcome to JavaRanch, Dustin!

First: When you post code, please UseCodeTags (←click). I've added them for you this time - see how much better it looks?

Also: Be sure to read the bit about not writing long lines; yours are fine, but if they get much longer, they can screw up the window formatting here, making threads difficult to read.

As to your errors: I'm a bit mystified too, because that line 15 looks perfectly fine to me.

Are you sure you're compiling this code? Because the error, if you've in fact copied it exactly as it appeared, seems to be complaining about
  number - (int) (Math.random() * 10)
which is NOT what your line 15 contains.

It is, however, correct for the line it's displaying: that first '-' should almost certainly be a '='; and it is indeed missing a semicolon.

But it doesn't appear to be line 15 from the above code. Are you sure it's not a compile of your Player class that you're looking at?

HIH

Winston
 
Dustin Douglas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply!

It's driving me nuts as well. Here's line 15 versus what CMD reports reading when i run guessgame.java through javac

Original line 15: int targetNumber = (int) (Math.random() * 10);

vs

Javac's version: number - (int) (Math.random() * 10)

The player class is compiled in a separate file which I haven't even created yet. The game should randomly generate the target number for the players to guess. In the player class file, it will have similar instructions "number = (int) (Math.random() * 10)" to generate each player's guess though. At least that's how it appears in this text!

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dustin Douglas wrote:It's driving me nuts as well. Here's line 15 versus what CMD reports reading when i run guessgame.java through javac
Original line 15: int targetNumber = (int) (Math.random() * 10);
vs
Javac's version: number - (int) (Math.random() * 10)

The player class is compiled in a separate file which I haven't even created yet.

I hate to say, but that's simply not possible:

1. javac isn't going to read a line "differently" to what you supplied. If it did, Oracle would have been inundated with complaints long before now.

2. If you haven't created/compiled your Player class, then you should get an error at line 2.

My advice: go back over what you've copied and/or done very carefully, because that error message is NOT coming from line 15 of the code you've shown us.

HIH

Winston
 
Dustin Douglas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ha!

First, I really appreciate your time and patience. My lack of understanding how Java works with multiple files at once is exactly the issue, and you even eluded to the resolution of the problem already.

I was getting the compiling error when I attempted to compile "guessgame.java"

I did not realize that I had created a Player.java file where I defined how each player would guess. I did not realize that running "javac guessgame.java" would involve anything other than that EXACT file "guessgame." I also forgot I even made the player class (as I said it was like 4 lines of code). There, I saw I did leave out the semicolon and had a - instead of an = sign. As with most software issues, it's that darn user causing ALL the problems

So, lesson learned. As you pointed out, the class needed to be defined for it to make it past line 2. I was started to get stumped at how Java was just allowing it to compile without understanding the class, perhaps voodoo or sorcery. No no, it turns out I'm just a dope!

Thanks for your input. I tried to make the Player class again and when saving it said "do you want to overwrite?" That's when I realized it had been sitting in my working folder all long, full of errors!



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dustin Douglas wrote:First, I really appreciate your time and patience...

No probs. It's what we're here for.

My lack of understanding how Java works with multiple files at once is exactly the issue...

And I should probably have said something about it. To be honest, it's been so long since I compiled from the command line that I'd forgotten about that little wrinkle.

However, don't you stop doing it for a while yet. There's always a bit of head-bashing when you start, but the command line is definitely the best place to learn about compiling (and jar-ing, when you get to that point).

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!