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) {
}
}
}
Related Tutorials/Questions & Answers:
Please provide coding for following problemPlease 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 problemPlease 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 Please provide the codePlease provide the code
Write a program to recursively download the web pages available in the given site URL
Please provide the codePlease 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
problem in codingproblem 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 codePlease 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 pleaseProvide 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 codingproblem 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
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
Problem about coding in ajvascriptProblem 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
coding problem - JSP-Servletcoding problem dear sir i m very new to JSP sir i m getting
problem in dynamically changing the option of combobox from database(ms acess) .and after that option being selected for that the record from the databse should
coding problem - Java Beginnerscoding problem hi! i declared date as date datatype in oracle now i need to retrieve date from database to my java code.how can i
please help me urgent Hi Friend,
We are providing you a code that will retrieve
coding problem - Java Beginnerscoding problem hi friend!
Im new to jasper reports.how can i start that
coding inorder to generate reports.can u send some sample programs for reporting?im badly need some clearly mentioned example because im new to jasper
coding problem - JSP-Servletcoding problem Dear sir
i m very new to JSP .I am using two conbobox .if any option is selected in first combobox then only its data from...' keyword in the combobox to call the function in javascript.
the
following code
coding problem - Java Beginnerscoding problem i used netbeans software to develop frame.now i need to add values to JList from ArrayList object Hi Friend,
Try the
following code:
import java.util.*;
import javax.swing.*;
public class
coding problem - JSP-Servletcoding problem dear sir
will u plz tell me how can we delete a record from the database(MSaccess) when option in the combobox is selected(jsp) If u want to delete the record from database, by seleting
Begineer in coding please help meBegineer 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
coding problem - Java Beginnerscoding problem i had a big doubt.
public ArrayList readExpenseType() throws ClassNotFoundException, SQLException,Exception
{
DBConnection dbc = new DBConnection();
Connection con = dbc.getConnection();
ArrayList
Problem in coding - Java BeginnersProblem in coding How many times do you have to roll a pair of dice before they come up snake eyes? You could do the experiment by rolling the dice... friend,
Code to help in solving the
problem.
public class Stimulates
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
please help me in coding this given programplease 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 programplease 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 CAPTCHA PROBLEMPLEASE 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 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..,
coding problem in netbeans - Java Beginnerscoding problem in netbeans hi, i have just started programming in netbeans,i'm facing a
problem
in a java application i create a jframe class and jbutton and jtextfeild inside it
now i have created another class c1
Please Provide jquery inline form validationPlease 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
program codingprogram coding how to rearrange the array a[1]. . a[n] even elements and odd elements?
please give the example
coding program.please
provide the
coding for this
problem Java Coding Problem - Java BeginnersJava
Coding Problem Q.1 How to write a while loop that displays all even numbers till 40? Q.2 How to write a method that accepts an integer parameter and displays the divisors of that integer? Hi friend
Please Help To Solve My ProblemPlease Help To Solve My Problem PHP
I Have 6 Textbox and 1 ok button.
6 Textbox to Add the 6 Team Name.
Each Team Name In String(Like- A,B,C,D,E,F... This
Problem With If...Else If...Else Statement But When the More Team
Coding errors for printing function, please helpCoding errors for printing function,
please help Hello,
We, my...;
Please visit the
following link:
http://www.roseindia.net/java/example/java.... Can someone
please take a look and help me to fix this. Although
Struts2 Helloworld problem -Please help - Struts.
Please help me out in resolving this
problem and
provide me a pointer...Struts2 Helloworld
problem -
Please help Hi
I am a beginner...
problem I unable to execute the program successfully.
Please find my example
coding problem using vb and access - Securitycoding problem using vb and access i have
problem on
coding using vb... that program.so hard for me.i have no idea.
then i also have
problem on update,delete and add...
please help me.as soon as possible..
please