Tsp dba

Greenhorn
+ Follow
since Sep 21, 2014
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tsp dba

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
7 years ago