variable declaration not allowed here

Highlights Factory
 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?

sehe

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

variable declaration not allowed here

From Dev

Why is variable declaration not allowed here?

From Dev

Variable declaration not allowed in Java

From Dev

Java variable declaration not allowed

From Dev

"Declaration Not allowed here" and "Constant Expression" Error in C

From Dev

java.util.Scanner Object Declaration not Allowed Here

From Dev

Variable declaration not allowed. But there is no scope overlap

From Dev

Why is ;; allowed after a local variable declaration, but not after a field declaration?

From Dev

Why it is allowed to initialize static variable with non const here?

From Dev

Why isn't short variable declaration allowed at package level in Go?

From Dev

"AllowOverride not allowed here" in WAMP

From Dev

AllowOverride not allowed here

From Dev

"set" directive is not allowed here

From Dev

.htaccess: LogLevel not allowed here

From Dev

Array initializer is not allowed here

From Dev

Column not allowed here error

From Dev

"AllowOverride not allowed here" in WAMP

From Dev

Why is a forward declaration in a function declaration allowed?

From Dev

Declaration and declaration with definition. Why is this not allowed?

From Dev

Error: 'void' type not allowed here

From Dev

nginx "env" directive is not allowed here

From Dev

Apache error "AliasMatch not allowed here"

From Dev

cdef statement not allowed here for structure

From Dev

Android manifest attribute not allowed here

From Dev

NGINX - "server" directive is not allowed here

From Dev

Error: Parse Objects not allowed here

From Dev

Why is lambda function not allowed here?

From Dev

About "group function is not allowed here"

From Dev

Apache error "AliasMatch not allowed here"

Related Related

HotTag

Archive