• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java- Simple Dot Com Game help  RSS feed

 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!