Hi I have a homework assigment I\'m using introduction to java programming 9th e
ID: 3534972 • Letter: H
Question
Hi
I have a homework assigment
I'm using introduction to java programming 9th ed book it is java for beginner
(Process large dataset) A university posts its employees' sataries at http://cs.armstrong.edu/liang/data/Salary.txt.
Each line in the file consists of a faculty member's first name, last name, rank, and salary. Write the 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
Download the text filele from the URL provided in Programming Exercise
14.25 (Process large datasets). Write the program specified in that
exercise with the following additional requirements:
Let the user enter the name of the le 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 le that does not
exist, the program prompts the user to enter again a le name.
In order to catch the FileNotFoundException, you need to include
import java.io.FileNotFoundException;
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LecturerReader {
public static void main(String[] args) {
try {
Scanner inScanner = new Scanner(System.in);
Scanner fileScanner = new Scanner(new File("salary.txt"));
boolean found = false;
while(true) {
System.out.println("Enter the lecturer name");
String name = inScanner.next();
while(fileScanner.hasNextLine()) {
if(name.equalsIgnoreCase(fileScanner.next())){
System.out.println(name + " " + fileScanner.next() + " " + fileScanner.next() + " " + fileScanner.next()) ;
found = true;
break;
}
}
if(found) {
break;
}
else {
System.out.println("Lecturer not found");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
output:
Enter the lecturer name
FirstName86
FirstName86 LastName86 associate 77841.89