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

1.Provide one to two sentence answers to each of the following questions: What h

ID: 3685238 • Letter: 1

Question

1.Provide one to two sentence answers to each of the following questions:

What happens if you try to open a file for reading that does not exist?  

What happens if you try to open a file for writing that doesn't exist?  

What happens if you try to open a file for writing on a device that is write-protected (such as a DVD-ROM)?

2.Provide printf statements for each of the following. You should choose appropriate variable names given the requirements:

Display the first 15 characters of a student's last name left justified, and the student's GPA to one decimal place. The names and GPAs should line up properly in columns if you print multiple students.

Display a tax rate as a percentage to 2 decimal places, a total dollar amount purchased for purchases up to $1,000,000.00 (include the commas), and the amount of tax on that purchase (also in dollars and cents with commas). Make sure that all of the amounts line up properly in columns.

3.Implement (i.e., write the body of) the following method, countLines, that takes a String as a parameter. The method should open the file named by that String, count the lines in the file, close the file, and return the number of lines of text it found in that file.

4.Implement a new version of the countLines method that catches the possible IOException exception and outputs an error message if such an exception is raised.

Explanation / Answer

public class Test {

public static void main(String[] args) {

String filename = "c:\abc.txt";

File inFile = new File(filename);

try {

PrintWriter out = new PrintWriter(filename);

} catch (FileNotFoundException e) {

e.printStackTrace();

  }

   }

}

The following error is shown in the console

java.io.FileNotFoundException: c:bc.txt (Access is denied)

at java.io.FileOutputStream.open(Native Method)

at java.io.FileOutputStream.(FileOutputStream.java:179)

at java.io.FileOutputStream.(FileOutputStream.java:70)

2.

3.   public int prev_Count_Lines(String fileName) throws IOException {
Scanner inputFile = new Scanner(new File(fileName));
int count = 0;
while(inputFile.hasNext()) {
inputFile.nextLine();
count++;
}
inputFile.close();
return count;
}

4. public int curr_Count_Lines(String fileName) {
int count = 0;
try {
Scanner inputFile = new Scanner(new File(fileName));
while(inputFile.hasNext()) {
inputFile.nextLine();
count++;
}
inputFile.close();
} catch (Exception e) {
System.out.println("Error in reading file");
}
return count;
}