Reputation: 197
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");
}
}
}
}
Upvotes: 1
Views: 140
Reputation: 1916
I think the easiest way is to use
answersList.contains(answer)
instead of
answersList.forEach(new String(equals(answer)))
Upvotes: 1
Reputation: 56469
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;
}
Upvotes: 1
Reputation: 543
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...
Upvotes: 1