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

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:\test");

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:\t $%.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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!