jQuery in Action, 2nd edition
The moose likes Beginning Java and the fly likes Head First Java- Simple Dot Com Game help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Head First Java- Simple Dot Com Game help" Watch "Head First Java- Simple Dot Com Game help" New topic
Author

Head First Java- Simple Dot Com Game help

John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Hi everyone, hope you can help.

Firstly, apologies for attaching all of the code and the length of the post!

I'm struggling to make the following compile and I am unsure why as I have checked my code against the code given in the book and can't see any problems.
The errors I get are:
illegal initializer for int
int locations = {randomNum, randomNum+1, randomNum+2};
and
setLocationCells(int[]) in SimpleDotCom cannot be applied to an (int)
theDotCom.setLocations(locations);





[edit]Change a // comment to /* */ to fit everything into screen width. CR[/edit]
[ December 19, 2008: Message edited by: Campbell Ritchie ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16681
    
  19

int locations = {randomNum, randomNum+1, randomNum+2};


The locations variable is declared as an int, it can't be assigned an int array. To do that you need to declare "locations" as an int array.


setLocationCells(int[]) in SimpleDotCom cannot be applied to an (int)
theDotCom.setLocations(locations);


The setLocationCells() method takes an int array. You can't pass it a regular int. If "locations" had been declared as an int array, as suggested as a possible fix to the previous error, then this error gets fixed too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Henry, you are a star!

Although, once I had added the [], I compared my code to the code in the book (for what seems like the 100th time!) and the brackets were there all along!

Thanks again!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
Welcome to JavaRanch

The comment I changed doesn't quite describe what you are doing.
You say "casting to int and multiplying by 5" which would give 0 in all instances.
What you are doing is "multiplying by 5 and casting to an int" which is actually correct. Math.random() actually returns a number between 0 and 0.9999999..... inclusive.

If you go through the methods of the Random class you can find an alternative way to get a (pseudo)random number between 0 and 4.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Head First Java- Simple Dot Com Game help
 
Similar Threads
Hi All, While working with Head first java, I got into a problem, please help me to solve this.....
HFJ 2nd - Problem with SimpleDotCom
getting exception in thread "main" java.lang.NoSuchMethodError: main error message
import java.io.*; problem
This code of mine is not working, Please help.... i am studying from Head first java