Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello Cheggs I need your guidance on a task I need to figure out. The book for t

ID: 3860589 • Letter: H

Question

Hello Cheggs I need your guidance on a task I need to figure out. The book for the class is Introduction to Java Programming, Comprehensive Version Y. Daniel Liang & Prentice Hall 11th Edition / 2018. We are currently working in Chapter 12. This chapers covers the followoing:

Exception Handling

Input Validation

File Class Input & Output

What issues do you find with the following code? Report back with line numbers and your fix for each issue you find.

1

2 File fileDir = new File("C: est");

3 File file = new File("C://test//test.txt");

4 if (fileDir.exists()) {

5    fileDir.mkdir();

6    System.out.println("The directory does not exist. Creating directory...");

7 } else {

8    System.out.printf("The directory does exist at this location: %s ", fileDir.getAbsolutePath());

9 }

10 if (file.exists()) {

11    System.out.println("The file does not exist. Creating file...");

12    file.createFile();

13    System.out.printf("The file now exists and is located here: %s ", file.getAbsolutePath());

14 } else {

15    System.out.printf("The file already exists and is located here: %s ", file.getAbsolutePath());

16 }

17

18 PrintWriter output = new PrintWriter(fileName);

19 double amount = 456.73;

20 for (int i = 1; i <= 10; i++) {

21    output.printf("Account %d: $%.2f" + System.getProperty("line.separator"), i, amount += 23.45);  

22 }

23 output.close();

24

25 try (Scanner input = new Scanner(file)) {

26    System.out.println("The contents of the file are listed below:");

27    while (input.hasNext()) {

28        String line = input.nextLine();

29        System.out.println(line);

30    }

31 }

32 input.close();

33

Explanation / Answer

Line 2 :
File name is invalid, since in C: est will be interpreted as tabspace.

Line 4 checks for existence of the file. (fileDir.exists()). And creates a new file using mkDir. But file should only be created if it doesn't exist. Condition must be if(!fileDir.exists())

Line 10 : Condition must be !file.exists() instead of file.exists()


Line 12 : createFile() is not a valid method. It must be createNewFile()


Line 18: fileName string in the PrintWriter object must be defined


Line 32 : input object doesn't exist.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote