Tacher Feedback: You are missing a few. Make sure the \"file\" has a correct pat
ID: 3699961 • Letter: T
Question
Tacher Feedback:
You are missing a few. Make sure the "file" has a correct path. Make sure there are enough } for the try blocks. And make sure you checking for the next line *hint properly.
This is the code that needs fixing:
?File fileDir = new File("C: est");
File file = new File("C://test//test.txt");
if (fileDir.exists())
{ fileDir.mkdir();
System.out.println("The directory does not exist. Creating directory...");
} else { System.out.printf("The directory does exist at this location: %s ", fileDir.getAbsolutePath());
}
if (file.exists()) { System.out.println("The file does not exist. Creating file...");
file.createFile();
System.out.printf("The file now exists and is located here: %s ", file.getAbsolutePath());
} else { System.out.printf("The file already exists and is located here: %s ", file.getAbsolutePath());
}
PrintWriter output = new PrintWriter(fileName);
double amount = 456.73;
for (int i = 1; i <= 10; i++) {
output.printf("Account %d: $%.2f" + System.getProperty("line.separator"), i, amount += 23.45);
}
output.close();
try (Scanner input = new Scanner(file)) {
System.out.println("The contents of the file are listed below:");
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line);
}
}
input.close();
These Are My Findings:
Line 2: File fileDir = new File("C:/test"); having the slash "" this way will read it as tab
Line 4: if(!fileDir.exists())
Line 10: if(!file.exists())
Line 12: not valid & should be createNewFile()
Line 18: PrintWriter output = new PrintWriter(file); the String in PrintWriter needed to be defined
Line 30: This line should close input input.close();
Explanation / Answer
Answer:
Along with the above finds below are few more findings
while (input.hasNextLine()) {
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.