• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While working from Head first java, I came across this problem... Please help

 
Mike Kris
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i understand what the problem is, but i am not getting an ideas as to how to fix it...
The code looks like this....



and the error is



D:\Java\Game>javac game.java
game.java:18: setLocationCells(java.util.ArrayList<java.lang.String>) in SimpleD
otCom cannot be applied to (int[])
theDotCom.setLocationCells(locations);
^
1 error


I understand that
int [] locations ... defines locations as an array of int.

public void setLocations(ArrayList<String> loc) ...
the args for setLocations is an object of the ArrayList<String> not an int array.

And thats the complaint in the error, but how do i fix this here....
Kindly type the code for me to fix it....

Thank you,
Mike
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turn the int[] into an ArrayList<String>.
 
Mike Kris
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob

So I gotta change it this way right??

Original: int[] locations = {randomNum, randomNum+1, randomNum+2};

Changed: ArrayList<String> locations = {randomNum, randomNum+1, randomNum+2};


But still this gives me the following error

D:\Java\Game>javac Game.java
Game.java:16: illegal initializer for java.util.ArrayList<java.lang.String>
ArrayList<String> locations = {randomNum, randomNum+1, randomNum+2};
^
1 error



Can you please help me by writing that part of the code for me.

Thanks a million Rob.
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I gotta change it this way right??

Original: int[] locations = {randomNum, randomNum+1, randomNum+2};

Changed: ArrayList<String> locations = {randomNum, randomNum+1, randomNum+2};



No.... take a look at the JavaDoc for the java.util.ArrayList class for details on how to use the class.

Can you please help me by writing that part of the code for me.


If you don't do it yourself -- by expecting people to write the code for you -- how are you ever going to learn the subject?

Henry
 
Mike Kris
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Henry. I understand. I will try to solve it myself.

Thanks for your advice,
Mike
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should check out the APIs of java.lang.Integer and/or java.lang.String for methods that can convert an int into a String.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic