| 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
|
|
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
|
|
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.
|
 |
 |
|
|
subject: Head First Java- Simple Dot Com Game help
|
|
|