Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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();
 }
}
share|improve this question

closed as off-topic by Vogel612, chillworld, Nikita Brizhak, palacsint, Simon André Forsberg Jun 20 at 10:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Vogel612, chillworld, Nikita Brizhak, palacsint, Simon André Forsberg
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
ad 1) This site is the wrong place to ask. ad 2) If you want help, please include error messages, as well as what you have already done and go check Stack Overflow. I bet you can solve the problem yourself with a little search-fu and thinking ;) –  Vogel612 Jun 20 at 9:33
add comment

Browse other questions tagged or ask your own question.