permaculture playing cards*
The moose likes Beginning Java and the fly likes While working from Head first java, I came across this problem... Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While working from Head first java, I came across this problem... Please help" Watch "While working from Head first java, I came across this problem... Please help" New topic
Author

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

Mike Kris
Greenhorn

Joined: Jun 08, 2009
Posts: 9
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

Turn the int[] into an ArrayList<String>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Kris
Greenhorn

Joined: Jun 08, 2009
Posts: 9
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
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Kris
Greenhorn

Joined: Jun 08, 2009
Posts: 9
Yes Henry. I understand. I will try to solve it myself.

Thanks for your advice,
Mike
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.
 
wood burning stoves
 
subject: While working from Head first java, I came across this problem... Please help
 
Similar Threads
Hi All, While working with Head first java, I got into a problem, please help me to solve this.....
Head First Java - pg 135 - ArrayList and Compile Errors
This code of mine is not working, Please help.... i am studying from Head first java
ArrayList problem. Head First Java, p. 138 - 139
getting exception in thread "main" java.lang.NoSuchMethodError: main error message