Pessoal estou estudando pelo livro Use a Cabeça Java mas ao chegar neste exemplo apresenta erro.
Os erros são: Syntax error on token “)” , : expected
Syntax error on token(s), misplaced construct(s)
O erro apresentado é no método checkYourSelf, na linha 10 ->for (int cell : locationCells){
class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
String checkYourSelf(String stringGuess){
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for (int cell : locationCells){
if (guess = cell){
result = "hit";
numOfHits++;
break;
}
}
if (numOfHits.equal == locationCells.length){
result = "kill";
}
System.out.println(result);
}
void setLocationCells (int[] locs){
locationCells = locs;
}
}
public class SimpleDotComTestDrive {
public static void main (String args[]){
SimpleDotCom dot = new SimpleDotCom();
int [] locations = {2,3,4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourSelf(userGuess);
}
}
[quote=leleca]Pessoal estou estudando pelo livro Use a Cabeça Java mas ao chegar neste exemplo apresenta erro.
Os erros são: Syntax error on token “)” , : expected
Syntax error on token(s), misplaced construct(s)
O erro apresentado é no método checkYourSelf, na linha 10 ->for (int cell : locationCells){
class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
String checkYourSelf(String stringGuess){
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for (int cell : locationCells){
if (guess = cell){
result = "hit";
numOfHits++;
break;
}
}
if (numOfHits.equal == locationCells.length){
result = "kill";
}
System.out.println(result);
}
void setLocationCells (int[] locs){
locationCells = locs;
}
}
public class SimpleDotComTestDrive {
public static void main (String args[]){
SimpleDotCom dot = new SimpleDotCom();
int [] locations = {2,3,4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourSelf(userGuess);
}
}
[/quote]
olá
seu codigo apresenta alguns problemas…
1 - numOfHits é uma variável de instância de tipo primitivo, portando não possui o atributo equals … o correto é:
if (numOfHits == locationCells.length) {
2 - Ainda vai existir um erro já que o método checkYourSelf() precisa retornar uma String. Portanto basta colocar na última linha do método um
Enquanto vocês respondiam dei mais uma olhada e até tinha consertado estes 2 últimos erros que Cadu disse mas continuo sem sucesso… Já conferi linha por linha e está idêntico ao livro, porém não dá certo.
Depois dos acertos ficou assim:
[code]public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
public void setLocationCells (int[] locs){
locationCells = locs;
}
public String checkYourSelf(String stringGuess){
int guess = Integer.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;
}
}[/code]
public class SimpleDotComTestDrive {
public static void main (String args[]){
SimpleDotCom dot = new SimpleDotCom();
int [] locations = {2,3,4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourSelf(userGuess);
}
}
Olha, eu sou muito mais iniciante que você… mas se eu me deparasse com um problema desses, primeiramente eu tentaria tirar o for each e fazer com o for primitivo ( for (int i = 0; i < x; i++) ). Se mesmo assim não funcionasse eu tentaria definir o tamanho fixo do vetor. Mas quem sou eu pra falar isso né
Dede é isso mesmo, o Carlos me disse que o problema era justamente por eu estar usando o java 1.4, troquei o for each pelo for antigo e deu certo. Agradeço a todos pela ajuda.
Mas por que você não atualiza para o 1.5?
O for each é muito bem aceito quando não se sabe o tamanho do array ou da coleção eu acho.
Em todo caso, que bom que conseguiu arrumar!