A Java question. Here is a text file containing the information about the salari
ID: 3825300 • Letter: A
Question
A Java question. Here is a text file containing the information about the salaries of the top 10 highest paid CEOs.
-------------------------------------------------------------------------------------------------------------------------
Write a program CEOSalaries to print a list which contains the ticker symbol, colon space, the name of the CEO, space, and the salary printed with a $ and comma separators. Use printf. The first record looks like this:
After all the records are printed, print: "Average salary: " and then the average salary with a $ and comma separators and 2 decimal places.
Note: You need to remove the $ and commas before using Integer.parseInt to convert a strings to an int.
The file name will be provided in the command line arguments.
Catch the exception if the file does not exist and print this message: No such file: filename
To process data in a file, you have to test the file to determine the format of the data. In this case, the name of the CEO can be either two or three words. Company names also varies in length. If the CEO name has three parts, one will be an initial, (like J. in the first entry). The initial may be either the first or the second name. Notice that the last name is the third name from the end.
your output should exactly look like I show follow:
Command line arguments:
-------------------------------------------------------------------------------------------------------------------------
Command line arguments:
Expected output VRX: J. Michael Pearson $143, 077, 442 MASI: Joe Ki ani $119, 222, 614 CTY: Lyndon R. Rive $77, 318, 016 GBL: Mario J. Gabelli $75,018, 176 PANN: Mark D. Mclaughlin $66, 606, 716 CBS Leslie Moonves $56, 773, 822 VIAB: Philippe P. Daum an $54, 154, 312 LNG: Charif Souki $54, 041,015 ORCL: Mark V. Hurd $53, 245, 128 REGN: Leonard S. Schleifer $47, 462, 526 Average salary: $74, 691, 976. 70Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Salary {
public static void main(String args[]){
BufferedReader br = null;
try {
br=new BufferedReader(new FileReader(args[0]));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(" ");
line = br.readLine();
}
String everything = sb.toString();
// System.out.println(everything);
printData(everything);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void printData(String everything) {
// TODO Auto-generated method stub
String[] array = everything.split("\s{2,}");
float sum=0;
int count=0;
for(int i=0;i<array.length-5;i=i+5){
System.out.println(array[i]+": "+array[i+2]+" "+array[i+4]);
sum = sum+getIntegerSalary(array[i+4]);
count++;
}
float avg = sum/count;
System.out.printf(" Average Salary: $ "+"%.2f",avg );
}
private static float getIntegerSalary(String string) {
// TODO Auto-generated method stub
String s= string.replaceAll(",","");
return Float.parseFloat(s.substring(1));
}
}
/* Here I have considered spaces more than 2 for getting a token which is name, salaray, year etc(ignoring the first line).*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.