这是我的代码:
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不能直接与Strings一起使用,这是我使用FileReader的原因,但是我仍然得到一个布尔值到字符串以及字符串到布尔值的错误。有人可以指出错误的正确方向吗?
答案 0 :(得分:7)
您必须用大括号括住作业,像这样:
while ((words = scan.readLine()) != null)
原因是Java中赋值运算符的优先级低于非品质运算符的优先级。实际上,赋值运算符的优先级是最低的。有关更多详细信息,请查看https://introcs.cs.princeton.edu/java/11precedence/