简体   繁体   English

如何比较ArrayList中的元素和特定的String?

[英]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 . 在下面的public void Run() ,我想用正确的答案验证用户输入String answer的答案: answersList.forEach(new String

I am trying to do this with a boolean . 我试图用boolean来做这个。 But I receive this error message: "Cannot resolve constructor 'String(boolean)'. How do I do to solve this issue? 但是我收到此错误消息:“无法解析构造函数'String(boolean)'。如何解决此问题?

Initialization of the private volatile boolean: 私有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... 并在BufferedReader,PrintStrem等之后......

  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); 出于同样的原因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? 但是我收到此错误消息:“无法解析构造函数'String(boolean)'。如何解决此问题?

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. 问题是你将"equals(answer)"的结果传递给了String的构造函数,因此没有String构造函数接受一个boolean值。


now to solve the issue you can either use lambda expression like this: 现在要解决这个问题你可以像这样使用lambda表达式:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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