private HashMap<String, String> answers;
public String generateResponse(HashSet<String> word)
{
for(String word : words)
String answer = answers.get(word);
if(answer != null){
return answer;
}else {
return standardAnswers();
}
}
I have imported HashMap. And I want for every word in HashSet to get it in HashMap<> answers. But in the for-each loop says: variable declaration not allowed here
How can I fix this?
A declaration is not a statement, so it's not allowed in that spot.
If it were, answer
would be out of scope at the if statement anyways.
Use a block scope to declare and use answer
:
private HashMap<String, String> answers;
public String generateResponse(HashSet<String> word)
{
for(String word : words)
{
String answer = answers.get(word);
if(answer != null){
return answer;
} else {
return standardAnswers();
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments