• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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
 
Sheriff
Posts: 21955
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 21955
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic