I am creating a database project in java using file handling. The program is working without compiling error. Following points are not working 1. File is storing only first record. (if program is running again it is overwriting file) 2. I want to display all records but the only first record is displaying with Execption please offer suggestions..
import java.io.*;
public class Student implements Serializable
{
private int roll; //To store Roll Number of Student
private String name; //To store name of Student
private int[] marks = new int[5]; //To store marks in 5 Subjects
private double percentage; //To store percentage
private char grade; //To store grade
public Student() //Default Constructor
{
roll = 0;
name = "";
for(int i = 0 ; i < 5 ; i++)
{
marks[i] = 0;
}
percentage = 0;
grade = ' ';
}
public Student(int roll , String name ,int[] marks) //Parametrised Constructor
{
setData(roll,name,marks);
}
public void setData(int roll , String name ,int[] marks) // Function to set values to data members
{
this.roll = roll;
this.name = name;
for(int i = 0 ; i < 5 ; i++)
{
this.marks[i] = marks[i];
}
cal();
}
private void cal() //Function to calculate Percentage and Grade
{
int sum = 0;
for(int i = 0 ; i < 5 ;i++)
{
sum = sum + marks[i];
}
percentage = sum/5;
if(percentage>85)
grade = 'A';
else if(percentage > 70)
grade = 'B';
else if (percentage >55)
grade = 'C';
else if (percentage > 33)
grade = 'E';
else
grade = 'F';
}
public char getGrade() //Function returning Grade
{
return grade ;
}
public double getPercentage() //Function returning Percentage
{
return percentage;
}
public String toString() //Function returning complete Information of Student
{
return (String.format("Roll Number : %4d, Name : -%15s Percentage : %4.1f Grade : %3s", roll, name, percentage, grade));
}
}
second file
import java.io.*;
public class FileOperation
{
public static void writeRecord(ObjectOutputStream outFile, Student temp) throws IOException, ClassNotFoundException
{
outFile.writeObject(temp);
outFile.flush();
}
public static void showAllRecords(ObjectInputStream inFile) throws IOException, ClassNotFoundException
{
Student temp = new Student();
while(inFile.readObject() != null)
{
temp = (Student)inFile.readObject();
System.out.println(temp);
}
}
}
main file (only two options are working)
import java.util.*;
import java.io.*;
public class Project
{
static public void main(String[] args) throws IOException,ClassNotFoundException
{
ObjectInputStream inFile;
ObjectOutputStream outFile;
outFile = new ObjectOutputStream(new FileOutputStream("info.dat"));
inFile = new ObjectInputStream(new FileInputStream("info.dat"));
Scanner var = new Scanner(System.in) ;
int roll;
String name;
int[] marks = new int[5];
int chc = 0;
Student s = new Student();
while(chc != 6)
{
System.out.print("\t\tMENU\n\n");
System.out.print("1.Add New Record\n");
System.out.print("2.View All Records\n");
System.out.print("3.Search a Record (via Roll Number) \n");
System.out.print("4.Delete a Record (via Roll Number) \n");
System.out.print("5.Search a Record (via Record Number)\n");
System.out.print("6.Exit\n");
System.out.print("Enter your choice : ");
chc = var.nextInt();
switch(chc)
{
case 1: System.out.print("\nEnter Roll number of Student : ");
roll = var.nextInt();
System.out.print("\nEnter Name of Student : ");
name = var.next();
System.out.println("\nEnter marks in 5 subjects \n");
for(int i = 0 ; i < 5 ; i++)
{
System.out.print("Enter marks in subject " + (i+1) + " ");
marks[i] = var.nextInt();
}
s.setData(roll , name , marks );
System.out.println("\n Adding Record to file \n");
System.out.printf("Record \n " + s);
System.out.println("\n\n");
FileOperation.writeRecord(outFile,s);
System.out.println("Record Added to File\n ");
break;
case 2 :System.out.println("All records in File \n");
FileOperation.showAllRecords(inFile);
break;
default:System.out.println("Wrong choice ");
}
}
outFile.close();
inFile.close();
}
}