• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Head First Java- Simple Dot Com Game help

 
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 ]
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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!
 
Marshal
Posts: 64686
225
  • 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!