Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

SimpleDotCom ArrayList test

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,

I am new to Java. I was trying an example in Head First Java book. this is Simpledotcom example. SimpleDotCom give me error of saying it cannot accept the int array to ArrayList object in method CheckYourSelf method. Does anybody know how to pass this to a ArrayList.

I am not able to figure out how to do it. Could anybody help me please.


Below is the code


public class SimpleDotComGame {

public static void main(String[] args) {

int numOfGuesses = 0;

GameHelper helper = new GameHelper();


DotCom theDotCom = new DotCom();

int randomNum = (int) (Math.random() * 5);

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

theDotCom.setLocationCells(locations);

boolean isAlive = true;


while(isAlive == true) {
String guess = helper.getUserInput("enter a number");

String result = theDotCom.checkYourself(guess);

numOfGuesses++;


if (result.equals("kill")) {
isAlive = false;

System.out.println("you took " + numOfGuesses + " guesses");
}// close if
}// close while

}// close main

}// close class

===
import java.io.*;

public class GameHelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0 ) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);

}
return inputLine;
}
}
==
import java.util.ArrayList;

public class DotCom {

private ArrayList<Integer> locationCells;


public void setLocationCells(ArrayList<Integer> loc) {
locationCells = loc;
}

public String checkYourself(String userInput) {

String result = "miss";

int index = locationCells.indexOf(userInput);

if (index >= 0) {

locationCells.remove(index);

if (locationCells.isEmpty()) {
result = "kill";
} else {
result = "hit";

} //close if
} //close outer if

return result;
}// close method

public void setLocationCells(int[] locations) {
// TODO Auto-generated method stub

}
} //close class
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int is a primitive datatype.
ArrayList accepts only objects not primitives.



Hence you got the error.Figure out how to convert int to Integer Object and pass the same to Arraylist.
(or)
Convert your integer array to Arraylist.
reply
    Bookmark Topic Watch Topic
  • New Topic