Please provide coding for following problem

Ads
 

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
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a program that displays the following table (note that 1 mile is 1.609 kilometers). Miles Kilometers
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a program that prompts the user to enter the number of students and each studentā??s name and score, and finally displays the name of the students
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a program that reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Suppose that the tuition for a university is RM10,000 this year and increases 5% every year. Write a program that computes the tuition in ten years and the total cost
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a function called ChangeToNext that take in an uppercase character parameter ch. The function returns the next uppercase character if ch is between 'A' and 'Y
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a class that contains the following two methods: /* converts from inches... that invokes these methods to display the following table: Inches Centimeters
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Body Mass Index(BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters
please provide the code
please provide the code   Develop a multi-client chat application
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
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   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 provide option for full materials download...
Please provide option for full materials download...   Please provide option for full materials download ... i dont have internet connection in my home. plz provide offline tutorial option
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
How to build .XML file, please provide an example.
How to build .XML file, please provide an example.  Hello, I want to know how to build .XML file. If you can provide an example, that would be great... Thanks
Please..please provide me a good Hibernate in Spring example
Please..please provide me a good Hibernate in Spring example  Hi, Can anyone please provide me a Hibernate in Spring example with simple explanation... hope the following links would be very useful to you: Hibernate Spring Example
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
program coding
program 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
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

Ads