Friday, December 2, 2011

Program reading Student Marks from file and Displays Who got Maximum Marks

Program reading Student Marks from file and Displays Who got Maximum Marks: =============================================================
This program will read  student details from data.txt file, and displayas
the details of student who got maximum marks.

data.txt
101,Arun,90,95,89
102,Ram,96,50,56
103,Shekar,98,78,89

MaxMarks.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class MaxMarks {
 public static void main(String[] args) throws Exception
 {
  List list=new ArrayList();

  File f=new File("data.txt");
  FileReader fr=new FileReader(f);
  BufferedReader br=new BufferedReader(fr);
  String str=null;

  while((str=br.readLine())!=null){

   StringTokenizer st=new StringTokenizer(str,",");
   while(st.hasMoreTokens())
   {
    int sid=Integer.parseInt(st.nextToken());
    String sname=st.nextToken();
    double[] marks=new double[3];
    marks[0]=Double.parseDouble(st.nextToken());
    marks[1]=Double.parseDouble(st.nextToken());
    marks[2]=Double.parseDouble(st.nextToken());
 
    double totMarks=marks[0]+marks[1]+marks[2];
    System.out.println(totMarks);
    
    Student stu=new Student(sid, sname, marks, totMarks);
    list.add(stu);
   }
  }

  Student s1=list.get(0);
  for(Student s2:list)
  {
      if(s1.getTotMarks() < s2.getTotMarks())
           s1=s2;
  }
  System.out.println("Max Marks Obtained by");
  System.out.println(s1);
 }
}
Student.java

import java.util.Arrays;
public class Student {
 int sid;
 String sname;
 double marks[];
 double totMarks;
 public Student() {
  super();
 }
 public Student(int sid, String sname, double[] marks, double totMarks) {
  super();
  this.sid = sid;
  this.sname = sname;
  this.marks = marks;
  this.totMarks = totMarks;
 }
 public int getSid() {
  return sid;
 }
 public void setSid(int sid) {
  this.sid = sid;
 }
 public String getSname() {
  return sname;
 }
 public void setSname(String sname) {
  this.sname = sname;
 }
 public double[] getMarks() {
  return marks;
 }
 public void setMarks(double[] marks) {
  this.marks = marks;
 }
 public double getTotMarks() {
  return totMarks;
 }
 public void setTotMarks(double totMarks) {
  this.totMarks = totMarks;
 }

 @Override
 public String toString() {
  return "sid=" + sid + ", sname=" + sname + " marks=" + Arrays.toString(marks) + ", totMarks=" + totMarks ;
 }
}

No comments:

Post a Comment

Search This Blog