Dead-Code
is which never gets executed, for example:
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
. 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
.
if-else
would have made sense if scanner
instance is passed as argument.
To solve this, else
should be removed!
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();