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

Hello Cheggs I submitted the below task to my instructor with what I thought wer

ID: 3866232 • Letter: H

Question

Hello Cheggs I submitted the below task to my instructor with what I thought were the issues in the code. Here are his comments in BOLD

The problem with line 2 is not the file name. What is the issue with it? Think "direction" of something (hint). Line 3 has the same issue but not direction, quantity of the "something" mentioned in line 3 comment above.

Line 18. What goes there has already been defined. What name was it given towards the top of the code?

Line 32 is not an object. That is used to close the input which at that point was open. It should remain.

You are missing one line that is between 18 and 30.

You got lines 4, 10, & 12 correct.

Below are my answers:

I found the following issues below:

I found an issue on Line 2
File name is invalid, ( in C: ) This will be looked at as extra space

I found an issue on Line 4

This line of code checks if the file exists. (fileDir.exists()). It should be if(!fileDir.exists()) if the file mkDir doesn’t exist one is created, but the file should only be created if the file doesn’t exist.

I found an issue on Line 10

This snippet of code should be !file.exists() and not file.exists()

I found an issue on Line 12

createFile() This isn’t a valid method. It should be createNewFile()

I found an issue on Line 18

The fileName string in the PrintWriter object needs to be defined

I found an issue on Line 32  

The input object doesn't exist.

I need your guidance Chegg to help me find the issue based on his comments Below is the task I was working on. 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 chapter covers the following:

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

Noticed Points :

Line2 : File path should be C://test

Line4 : This line of code checks if the file exists. (fileDir.exists()). It should be if(!fileDir.exists())

Line10 : This snippet of code should be !file.exists() and not file.exists()

Line 12: createFile() This isn’t a valid method. It should be createNewFile()

Line 18 : pass file object name to PrintWriter constructor.It should be like PrintWriter output = new PrintWriter(file);

Line 25 :try block does not take any parameters and it must have a catch block followed .code snippet shown below

  

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();

}

catch(Exception e)

{

}

Line 32 : input object to close is not in the scope of try block.Place in try block.

Finally for avoiding all types exceptions related to files use "throws" for exception handling as below statment

public static void main(String []args) throws Exception

Finally Executing code :

import java.util.*;

import java.io.*;

public class files

{

public static void main(String []args) throws Exception

{

File fileDir = new File("C://test");

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

if (!fileDir.exists()) {

fileDir.mkdirs();

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.createNewFile();

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(file);

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();

}

catch(Exception e)

{

}

}

}

out put :

C:UsersAshokDesktopCheggjava>java files
The directory does not exist.
Creating directory...
The file does not exist.
Creating file...
The file now exists and is located here:
C: est est.txt
The contents of the file are listed below:
Account 1: $480.18
Account 2: $503.63
Account 3: $527.08
Account 4: $550.53
Account 5: $573.98
Account 6: $597.43
Account 7: $620.88
Account 8: $644.33
Account 9: $667.78
Account 10: $691.23

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