I need help with a bit of java code!?

My book wants me to program a basic battleship game. The page I'm on is entitled: The checkYourself() method. This is the exact code from the book as well as what I tried to compile

public String checkYourself(String stringGuess) {

int guess = Interger.parseInt(stringGuess);

String result = "miss";

for (int cell : locationCells) {

if (guess == cell) {

result = "hit";

numOfHits++;

break;

}

}

if (numOfHits == locationCells.length) {

result = "kill";

}

system.out.println(result)

return result;

}

But it will not compile. The next section of code needs to call on this method, but I really have no Idea what I'm doing. Here's what the errors are when I attempt to compile. If you could help me figure out what I'm doing wrong and tell me why that's wrong, it would be helpful. Thanks in advance!

checkYourself.java:1: error: class, interface, or enum expected

public String checkYourself(String stringGuess) {

^

checkYourself.java:3: error: class, interface, or enum expected

String result = "miss";

^

checkYourself.java:4: error: class, interface, or enum expected

for (int cell : locationCells) {

^

checkYourself.java:7: error: class, interface, or enum expected

numOfHits++;

^

checkYourself.java:8: error: class, interface, or enum expected

break;

^

checkYourself.java:9: error: class, interface, or enum expected

}

^

checkYourself.java:13: error: class, interface, or enum expected

}

^

checkYourself.java:16: error: class, interface, or enum expected

}

^

8 errors

Update:

Ok thank you guys so much. After reading this and going a little further in my book it uses the method in a class. I'm trying to compile that class at the moment, here's the code it gave me to type:

public class SimpleDotCom {

int[] locationCells;

int numOfHits = 0;

public void setLocationCells(int[] locs) {

locationCells = locs;

}

public String checkYourself(String stringGuess) {

int guess = Interger.parseInt(stringGuess);

String result = "miss";

for (int cell : locationCells) {

if (guess == cell) {

result = "hit";

numOfHits++;

break;

}

}

if (numOfHits == locationCells.length) {

result = "kill";

}

System.out.println(result);

return result;

}

}

And here's the error:

SimpleDotCom.java:8: error: cannot find symbol

int guess = Interger.parseInt(stringGuess);

^

symbol: variable Interger

location: class SimpleDotCom

1 error

Mind telling we what it means by cannot find symbol?

4 Answers

Relevance
  • 8 years ago
    Favorite Answer

    That code is a fragment that needs to be places inside of another class. It's a member function (not static, like main(),etc. and accesses non-local variables locationCells and numHits, so the class must have those variables defined in it.

    The top level items in a Java source file are either types, or interfaces; and a type is either a class type or an enum type. That's what the message is telling you. Before you can define anything else, you must begin the definition of the type or interface that the definition belongs in. That's what the message is telling you with "class, interface or enum expected".

    Edit: You misspelled "Integer" as "Interger". Integer is a built-in class, Interger is undefined...and that's what "cannot find symbol" means. Usually, either something is either misspelled (including wrong capitalization...Java is case-sensitive) or the required class was not imported at the top of the source file.

  • 8 years ago

    This looks like an extract showing a particular method. It's not a complete program in itself, so it won't compile. (The first error is relevant; the others are all triggered as a consequence of the first.)

    ----------

    Ref your Additional Details

    Read the error message more carefully... "symbol: variable Interger"

    Doh!

  • zuk
    Lv 4
    4 years ago

    You fairly plenty have it superb. once you want an occasion of an merchandise you will write: worker emp = new worker("1234", "Jane Doe"); Java will allocate some memory to hold the hot worker merchandise. it is going to then initialise that memory by using calling the constructor with the parameters you have surpassed. a classification could have quite a few constructors, pointed out by using having diverse parameters. classification could have large- and sub-classes, so it is a hazard for a sub-classification to bypass parameters parameters to its large-classification, e.g.: classification DummyEmployee extends worker { public DummyEmployee() { large("0", "no one"); } }

  • 8 years ago

    That is a method / function. It needs a class to be inside of.

Still have questions? Get your answers by asking now.