死代码java eclipse
我试图检查用户给出的单词是否已经存在于文本文件中,或者它的子字符串是否已经存在。这是我的密码:死代码java eclipse,java,dead-code,Java,Dead 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 =
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"));
String ans=null;
扫描器=空;
做
{
System.out.print(“请输入新单词:”);
字符串Nword=scan.next();
System.out.print(“及其相应提示:”);
字符串Nhint=scan.next();
单词=新词(Nword,Nhint);
File File=新文件(“C:\\Users\\Charbel\\Desktop\\Dictionary.txt”);
createNewFile();
扫描仪=新扫描仪(文件);
如果(扫描器!=null)
{
弦线;
while(scanner.hasNext()){
line=scanner.next();
对于(int i=0;i
扫描器
已初始化,永远不能为null
,因此永远不会到达else
语句
见:
抛出:
-如果找不到源
因此,如果文件
不存在,扫描器
将不会为空
,您将有一个异常
扫描器
已初始化,永远不能为null
,因此永远不会到达else
语句
见:
抛出:
-如果找不到源
因此,如果文件
不存在,扫描器
将不会为空
,您将有一个异常。死代码
是永远不会执行的,例如:
if(true) {
// do something
}else {
// do something else <-- this is dead code, or else-block is dead code
}
死代码
是永远不会执行的代码,例如:
if(true) {
// do something
}else {
// do something else <-- this is dead code, or else-block is dead code
}
此语句正在此处创建新实例。因此:
if(scanner!=null)
永远不会为false
此语句正在此处创建新实例。因此:
if(扫描器!=null)
永远不会为假。也许你的扫描器永远不会为空
?看看扫描器
的文档。如果文件
不存在,它将抛出一个FileNotFoundException
,但它永远不会为空
。因此你的else
-语句永远不会执行,也不会停止o它是死代码
:-)在Java中,死代码是永远不会被执行的代码。也许你的扫描器永远不会null
?看看scanner
的文档。如果文件不存在,它将抛出FileNotFoundException
,但它永远不会null
。因此你的else
-语句i在Java中,死代码是永远不会被执行的代码。那么我怎么能说如果这个词不存在或者它的子串就写它呢?我怎么能说如果这个词不存在或者它的子串就写它呢如果这个词不存在或者它的子串不匹配,我怎么说呢?如果这个词不存在或者它的子串不匹配,我怎么说呢?如果这个词不存在或者它的子串不匹配,我怎么说呢?如果这个词不存在或者它的子串不匹配,我怎么说你想写呢仅当等于
或子字符串
匹配失败时,对吗?
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();
scanner = new Scanner(file);