我的代码为我的BufferedReader和FileReader返回一个字符串为boolean和boolean to string error,这是我的错误?

kgb*_*b86 2 java filereader bufferedreader

这是我的代码:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

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){
Run Code Online (Sandbox Code Playgroud)

我正在尝试从命令行接收文件,扫描它,然后在终端中逐行打印出文件所说的内容.我知道的BufferedReader不与弦乐,这是我之所以使用的FileReader直接工作了,但我仍然得到一个布尔值,字符串,字符串,布尔错误.有人能指出我找到这个错误的正确方向吗?

The*_*vić 7

你必须用大括号括起赋值,如下所示:

while ((words = scan.readLine()) != null)
Run Code Online (Sandbox Code Playgroud)

原因是Java中赋值运算符的优先级低于非质量运算符的优先级.事实上,赋值运算符的优先级是最低的.有关详细信息,请查看https://introcs.cs.princeton.edu/java/11precedence/