Java 我的代码正在为我的BufferedReader和FileReader返回字符串到布尔值和布尔值到字符串的错误,我的错误在哪里?

Java 我的代码正在为我的BufferedReader和FileReader返回字符串到布尔值和布尔值到字符串的错误,我的错误在哪里?,java,bufferedreader,filereader,Java,Bufferedreader,Filereader,这是我的密码: import java.io.File; import java.io.BufferedReader; import java.io.FileReader; public class SymbolBalance{ public static void main(String[] args) throws Exception{ File givenFile = null; String words = null; if(a

这是我的密码:

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;

public class SymbolBalance{
    public static void main(String[] args) throws Exception{
        File givenFile = null;
        String words = null;

        if(args.length > 0){
            givenFile = new File(args[0]);
        } else{
            System.out.println("Error! No file name was given!");
        }      
        BufferedReader scan = new BufferedReader(new FileReader(givenFile));

        while(words = scan.readLine() != null){
            System.out.println(words);
        }
        scan.close();
    }
}
这是我的错误:

codio@titanic-avenue:~/workspace$ javac SymbolBalance.java
SymbolBalance.java:21: error: incompatible types: boolean cannot
 be converted to String
            while(words = scan.readLine() != null){
                                      ^
SymbolBalance.java:21: error: incompatible types: String cannot
be converted to boolean
            while(words = scan.readLine() != null){

我试图从命令行中获取一个文件,扫描它,然后在终端中逐行打印出该文件的内容。我知道bufferedreader不能直接处理字符串,这也是我使用FileReader的原因,但我仍然会得到一个boolean-to-string和string-to-boolean错误。有人能给我指出正确的方向来发现这个错误吗?

你必须用大括号把作业括起来,如下所示:

while ((words = scan.readLine()) != null)

原因是Java中赋值运算符的优先级低于非优质运算符的优先级。事实上,赋值运算符的优先级是最低的。有关更多详细信息,您可以查看

将您的代码以文本形式发布在此处。现在,我们将编辑它。