[英]Dead code java eclipse
I am trying to check if the word given by the user already exists in the text file or a substring of it already exists. 我正在尝试检查用户给定的单词在文本文件中是否已经存在或它的子字符串是否已经存在。 Here's my code:
这是我的代码:
String ans = null;
Scanner scanner = null;
do
{
System.out.print("Please enter a new word: ");
String Nword = scan.next();
System.out.print("And its appropriate Hint: ");
String Nhint = scan.next();
Word word = new Word(Nword , Nhint);
File file = new File("C:\\Users\\Charbel\\Desktop\\Dictionary.txt");
file.createNewFile();
scanner = new Scanner(file);
if (scanner != null)
{
String line;
while (scanner.hasNext()) {
line = scanner.next();
for(int i = 0 ; i<line.length(); i++)
if ((line.equals(Nword)) || (Nword.equals(line.substring(i))))
{
System.out.println("The word already exists.");
break;
}
}
}
else
{
FileWriter writer = new FileWriter(file , true);
writer.write(word.toString());
writer.write(System.lineSeparator());
writer.flush();
writer.close();
System.out.println("Your word has successfuly added.");
System.out.print("\nWould you like to add another word ?\nPress 0 to continue.");
ans = scan.next();
}
} while(ans.equals("0"));
Eclipse said that the statements after the else
condition are "Dead Code" and I don't know why. Eclipse表示
else
条件之后的语句是“死代码”,我不知道为什么。
scanner = new Scanner(file);
scanner
is initialized, can never be null
, so the else
statement will never be reached. scanner
已初始化,永远不能为null
,因此永远不会到达else
语句。
See the constructor : 参见构造函数 :
Throws:
抛出:
FileNotFoundException
- if source is not foundFileNotFoundException
如果找不到源
So if the file
doesn't exists, scanner
won't be null
, you'll have an exception . 因此,如果
file
不存在, scanner
将不会为null
,那么您将有一个异常 。
scanner = new Scanner(file);
This statement is creating a new instance here. 该语句在这里创建一个新实例。 So this:
if (scanner != null)
will never be false. 所以这就是:
if (scanner != null)
永远不会为假。
Dead-Code
is which never gets executed, for example: Dead-Code
永远不会执行,例如:
if(true) {
// do something
}else {
// do something else <-- this is dead code, or else-block is dead code
}
In your case since Scanner
is getting created before if(scanner != null)
there is no way of execution of associated else
. 在您的情况下,由于
Scanner
是在if(scanner != null)
之前创建的,因此无法执行关联else
。 If Scanner
creation fails error will be thrown again in which else
will not be executed, hence from compiler point-of-view no chance of else
block getting executed hence dead-code
. 如果
Scanner
创建失败错误将被再次抛出在else
不会被执行,因此,从编译器的点的视图没有机会else
块得到执行,因此dead-code
。
if-else
would have made sense if scanner
instance is passed as argument. if-else
将scanner
实例作为参数传递, if-else
会有意义。
To solve this, else
should be removed! 要解决此问题,
else
应删除!
Following should correct your code: 以下应纠正您的代码:
scanner = new Scanner(file);
FileWriter writer = new FileWriter(file, true);
if (scanner != null) {
String line;
while (scanner.hasNext()) {
line = scanner.next();
for (int i = 0; i < line.length(); i++)
if ((line.equals(""))
|| ("".equals(line.substring(i)))) {
System.out.println("The word already exists.");
break;
} else {
writer.write(word.toString());
writer.write(System.lineSeparator());
writer.flush();
System.out.println("Your word has successfuly added.");
System.out.print("\nWould you like to add another word ?\nPress 0 to continue.");
ans = scan.next();
}
}
}
writer.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.