Please provide coding for following problem
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.
View Answers
April 21, 2011 at 11:29 AM
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) {
}
}
}
Ads
Related Tutorials/Questions & Answers:
Please provide coding for following problem
Please provide coding for
following problem
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
Please provide the coding for this problem
Please provide the
coding for this problem
Please provide the
coding for the
following problem. Write a "hangman" game that randomly generates a word and prompts the user to guess one letter at a time. Each letter in the word
Advertisements
problem in writing coding. Please help me.
problem in writing
coding.
Please help me. Hi sir, my name is Logeswaran. I have
problem in writing JSP
coding for my university assigments. Actually I'm doing a Voting System. I need to know how to write JSP
coding
problem in coding
problem in coding i have a
following code which display the contents of the text file but not other program like java or any c program is there any handler to open the program files other than Filereader.
FileReader fr = new
Please provide the code
Please provide the code
Write a program to recursively download the web pages available in the given site URL
Please provide the code
Please provide the code
Program to find depth of the file in a directory and list all files those are having more number of parent directories
Please provide the code
Please provide the code
Create a file system:
We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only
jsp coding please.
jsp
coding please. hi sir, my name is logeswaran. I have a
problem... list.
please show me how to do it? thank you.
By the way, I'm using access database and
coding JSP.
Follow these steps:
1)Go to the start->
Provide the code please
Provide the code please
Program to calculate the sum of two big numbers (the numbers can contain more than 1000 digits). Don't use any library classes or methods (BigInteger etc
problem i coding
problem i coding i have a
problem in
coding a combobox containing a number of *.java program on click one of them that program should compile and run and display the result in other window
Problem about coding in ajvascript
Problem about
coding in ajvascript I created a three radio buttons in the
following way
How many iphone styles are present in the image... the option selected is true or false. Can
please help me in getting a code using
Please give me coding for this..
Please give me
coding for this.. Write an application that inputs one number consisting of five digits from the user.separates the number ibto its individual digits and prints the digits separated from one another by three
please help me to this problem..
please help me to this
problem.. i wrote a program like keyboard and i used [JTextFiled] means that just you can write inside the program (i.e inside the JTextField) but i want to make this program to let me write where i
Begineer in coding please help me
Begineer in
coding please help me I have
Person class{
int pid;
String Pname;
//and have a Address filed of type Address not String!!!
Address... address to a person object..
please help me
PLEASE HELP CAPTCHA PROBLEM
PLEASE HELP CAPTCHA PROBLEM how can i make the captcha refresh autometically ????
please help
this is
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing"%>
<%@ Import Namespace
please help me in coding this given program
please help me in
coding this given program consider the below series:
1,2,1,3,2,5,3,7,5,11,8,13,13,17,.....
this series is a mixture of 2 series... in this series.
(i m unable to code this program..
please provide solution
please help me in coding this given program
please help me in
coding this given program consider the below series:
1,2,1,3,2,5,3,7,5,11,8,13,13,17,.....
this series is a mixture of 2 series... in this series.
(i m unable to code this program..
please provide solution
Please provide the java code for the given program.
Please provide the java code for the given program.
We need an application for managing an educational institute.
That application should
provide the details of
Students
Courses
Faculty
Fee details
etc..,
 
Please Provide jquery inline form validation
Please Provide jquery inline form validation <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content