
Please provide the coding for the following problem:
You will write a java program that will read data from a file. The data in the file will be: John Doe 75 Joe Blow 65 Mary Smith 80 John Green 82 Jill White 97
The program will rad the data into three separate arrays: Firstname array, LastName array, and a score array. The output will be the initials followed by their score in sorted order from top score to lowest score:
So the output will be: JW 97 JG 82 MS 80 JD 75 JB 65
I have the data file already written up, just unsure of as to how I am to write the program to run it.
Appreciate any assistance.

import java.io.*;
import java.util.*;
class ShowData implements Comparable {
String fname;
String lname;
int score;
public void setFname(String fname) {
this.fname = fname;
}
public String getFname() {
return fname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getLname() {
return lname;
}
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
public int compareTo(Object ob) throws ClassCastException {
if (!(ob instanceof ShowData))
throw new ClassCastException("Error");
int marks = ((ShowData) ob).getScore();
return this.score - marks;
}
}
class FileData{
public static void main(String[] args){
int j = 0;
ShowData data[] = new ShowData[5];
try {
FileInputStream fstream = new FileInputStream("C:/file.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
ArrayList list = new ArrayList();
while ((strLine = br.readLine()) != null) {
list.add(strLine);
}
Iterator itr;
for (itr = list.iterator(); itr.hasNext();) {
String str = itr.next().toString();
String[] splitSt = str.split(" ");
String firstname = "", lastname = "", score = "";
for (int i = 0; i < splitSt.length; i++) {
firstname = splitSt[0];
lastname = splitSt[1];
score = splitSt[2];
}
data[j] = new ShowData();
data[j].setFname(firstname);
data[j].setLname(lastname);
data[j].setScore(Integer.parseInt(score));
j++;
}
Arrays.sort(data,Collections.reverseOrder());
String strVal = "";
for (int i = 0; i < 5; i++) {
ShowData show = data[i];
String fname = show.getFname();
int score = show.getScore();
String lname = show.getLname();
String fn=fname.substring(0,1).toUpperCase();
String ln=lname.substring(0,1).toUpperCase();
String fl=fn+ln;
System.out.println(fl + "\t " + score);
}
} catch (Exception e) {
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.