1

I am currently working on a quiz game. In the following public void Run(), I want to validate the user input String answerwith 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");
         }
     }
   }
}
4
  • Which strings are you trying to compare? Commented Apr 7, 2017 at 19:55
  • 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 and even if you were to remove the "equals()" you'd still get an error because the forEach method accepts a Consumer. please specify all the code needed and what you want the end result to be. Commented Apr 7, 2017 at 20:01
  • just use List.contains(answer). Commented Apr 7, 2017 at 20:04
  • @JosefLundström see my answer. Commented Apr 7, 2017 at 20:11

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Ousmane for your answer. I did accept this answer because it was according to what I tried to achieve in the first place. Thank you all who wanted to help me...
@JosefLundström glad to know it helped! :).
1

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...

3 Comments

this wouldn't compile because the forEach takes as an argument a Consumer.
Why are you supplying it with a String then?
.anyMatch() returns a boolean value. as for the "forEach" a Consumer takes in an argument but doesn't return anything, that's basically what i am getting at.
1

I think the easiest way is to use

answersList.contains(answer) 

instead of

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.