简体   繁体   中英

How to compare an element from ArrayList with a specific String?

I am currently working on a quiz game. In the following public void Run() , I want to validate the user input String answer with the correct answers: answersList.forEach(new String .

I am trying to do this with a boolean . But I receive this error message: "Cannot resolve constructor 'String(boolean)'. How do I do to solve this issue?

Initialization of the private volatile boolean:

class Broadcaster extends Thread {
private volatile boolean check_point = true;

public boolean getFlag() {
    System.out.println("getFlag is running");
    return this.check_point;

}

And after BufferedReader, PrintStrem and so on...

  public void run() {
    while (true) {
        try {
            List<String> answersList = new ArrayList<>();
            try (Stream<String> answersStream = Files.lines(Paths.get(answersFile))) {
                answersList = answersStream
                        .map(String::toLowerCase)
                        .collect(Collectors.toList());
            } catch (IOException a) {
                a.printStackTrace();
                System.out.println("The timer isn't working correctly");
            }
            if (answersList.forEach(new String(equals(answer)))) { //ERROR MESSAGE
                write.println("Right Answer");
                points++;
                check_point = true;

            } else {
                write.println("Wrong Answer");
                check_point = false;
            }
        } catch (Exception e) {
            System.out.println("Some problem with the validation");
         }
     }
   }
}

I think the easiest way is to use

answersList.contains(answer) 

instead of

answersList.forEach(new String(equals(answer)))

For the same reason String s = new String(false); results in an error, you can't create a string object from a boolean value.

try this...

if (answersList.forEach(new String(Boolean.toString(equals(answer)))))

I feel like you're making what you're trying to do way more complicated than it has to be though...

But I receive this error message: "Cannot resolve constructor 'String(boolean)'. How do I do to solve this issue?

the problem is that you're passing the result of "equals(answer)" which is a boolean value to the constructor of the String hence there is no String constructor which takes a boolean value.


now to solve the issue you can either use lambda expression like this:

if(answersList.stream().anyMatch(str -> str.equals(answer))){
    write.println("Right Answer");
    points++;
    check_point = true;
}else{
    write.println("Wrong Answer");
    check_point = false;    
}

or simply:

if(answersList.contains(answer)){
    write.println("Right Answer");
    points++;
    check_point = true;
}else{
   write.println("Wrong Answer");
   check_point = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM