Download the text file from the URL provided in Programming Exercise 12.25 (Proc
ID: 3688022 • Letter: D
Question
Download the text file from the URL provided in Programming Exercise 12.25 (Process large dataset).
Write the program specified in that exercise with the following additional requirements:
– Let the user enter the name of the file to be read.
– Use a try-catch block to handle the FileNotFoundException displaying instead The file already exists..
– Use a second catch block to ignore any other exception thrown.
– Design your code so that, if the user enters a file that does not exist, the program prompts the user to enter again a file name.
Notes: In order to catch the FileNotFoundException, you need to include import java.io.FileNotFoundException;. If you prefer to scan directly from the web rather than downloading the file, Section 12.12 explains how.
*** EXERCISE 12.25
(Process large dataset) A university posts its employees' salaries at http://cs.armstrong.edu/liang/data/Salray.txt
Each line in the file consists of a faculty member's first name, last name, rank and salary. Write a program to display the total salary for assistant professors, associate professors, full professors, and all faculty, respectively, and display the average salary for assistant professors, associate professors, full professors, and all faculty, respectively.
(ALSO, ANSWER QUESTIONS ABOVE)
THANK YOU SO MUCH!!!!
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class ReadLargeDataSet {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null, fileScanner = null;
File file = null;
try {
scanner = new Scanner(System.in);
do {
System.out.print("Enter the name of file:");
String fileName = scanner.next();
try {
file = new File(fileName);
if (file.exists()) {
break;
} else {
throw new FileNotFoundException();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
} while (true);
double sumAssistProf = 0.0d, sumAssProfessors = 0.0d, sumFullProf = 0.0d, sumAllFaculty = 0.0d;
int countAssistProf = 0, countAssProfessors = 0, countFullProf = 0, countAllFaculty = 0;
fileScanner = new Scanner(file);
while (fileScanner.hasNext()) {
// associate,assistant,full
String firstName = fileScanner.next();
String larstName = fileScanner.next();
String rank = fileScanner.next();
double salary = fileScanner.nextDouble();
sumAllFaculty += salary;
countAllFaculty++;
if ("assistant".equalsIgnoreCase(rank)) {
sumAssistProf += salary;
countAssistProf++;
} else if ("associate".equalsIgnoreCase(rank)) {
sumAssProfessors += salary;
countAssProfessors++;
} else if ("full".equalsIgnoreCase(rank)) {
sumFullProf += salary;
countFullProf++;
}
}
System.out.println("Total salary for:");
System.out
.printf(" Assistant professors : %.2f, Associate professors: %.2f, Full professors: %.2f, All faculty: %.2f, ",
sumAssistProf, sumAssProfessors, sumFullProf,
sumAllFaculty);
System.out.println("Average salary for:");
System.out
.printf(" Assistant professors : %.2f, Associate professors: %.2f, Full professors: %.2f, All faculty: %.2f, ",
sumAssistProf / (double) countAssistProf,
sumAssProfessors / (double) countAssProfessors,
sumFullProf / (double) countFullProf, sumAllFaculty
/ (double) countAllFaculty);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
fileScanner.close();
scanner.close();
}
}
}
OUTPUT:
Enter the name of file:nofile.txt
java.io.FileNotFoundException
at ReadLargeDataSet.main(ReadLargeDataSet.java:23)
Enter the name of file:Salary.txt
Total salary for:
Assistant professors : 20246511.91,
Associate professors: 28844146.58,
Full professors: 35678051.41,
All faculty: 84768709.90,
Average salary for:
Assistant professors : 65949.55,
Associate professors: 83849.26,
Full professors: 102229.37,
All faculty: 84768.71,
NOTE: download Salary.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.