• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java problem correcting SimpleDotCom class.  RSS feed

 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I'm having a problem with the head first java book. Trying to make the change to the code (p160) which changed the simple arrays to ArrayLists. The below array in the SimpleDotCom Class contains the random numbers, these were previously held in an Integer array

public void setLocationCells(ArrayList<String> locs){
locationCells = locs;
}

But now they have been changed to be held in a String array, I can't perform (in the game class) the following calculation on the numbers to get 3 consecutive numbers from the random number generated.

int randomNum = (int) (Math.random() * 5+1);
System.out.println(randomNum);
ArrayList<String> locations = {randomNum, randomNum+1, randomNum+2};

theDotCom.setLocationCells(locations);


I'm tried populating the array with a for loop and adding a casted incremented value, but it wouldn't let me cast a int to String. I've also tried adding locations.add(String.valueof(randomNum) I'm a little confused as the book states that the ArrayList should be of type String and I'm not sure how to get these numbers stored in there.

Any help would be much appreciated.

Thanks,

Michael

 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, nevermind. I'm a clown, found the problem now and it was because of my syntax.

found the solution in the forums here:

http://www.coderanch.com/t/520316/java/java/Head-first-Java-telling-me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!