Javalearner2k
Javalearner2k

Reputation: 1

Show student records from input file

I am not able to read and add student records from an input file into an existing array.

The original array size is 10. When the array is full, its size is to be doubled.

If the input file contains 25 records, my array shows only the last 5 records. I know my array expansion coding is incorrect, and I am unable to resolve. Here are my classes:

import java.util.*;

public class ClassPeriod {
   private int              myNumStudents;
   private Student[]        myStudents;
   private String           myClassName;
   int N = 10;

   public ClassPeriod(String classname){
       myClassName = classname;
       myNumStudents = 0;
       myStudents = new Student[N];
   }

   // add the Student to the myStudents array. If the array is full, create a new
   // one twice the size of the current one. Update myNumStudents accordingly.
   public void addStudent(Student st){
        for (int i=0; i<1; i++){
            if (myNumStudents == 10 || myNumStudents == 20) {//student array size is 10, if 10 is reached, double its size
                N = 2*myNumStudents;
                myStudents = new Student[N];
            }
            switch (myNumStudents)
            {
                case 0: myStudents[0] = st; break;
                ...
                ...
                case 24: myStudents[24] = st; break;
                default: break; 
            }
            myNumStudents++;//increment myNumStudents by 1
        }
        for (int j=0;j<N;j++){
            System.out.println("Students: " + myStudents[j]);
        }
    }

    public Student[] getStudents(){
        System.out.println("myNumStudents: " + myNumStudents);
        Student temp[] = new Student[myNumStudents];
        for (int i=0; i<myNumStudents; i++){
            temp[i] = myStudents[i];
        }
        System.out.println("temp: " + temp.length);
        return temp;
    }

    public String toString(){
        String s = new String(myClassName + "\n");
        int i;
        for (i=0; i<myNumStudents-1; i++)
            s += myStudents[i].toString() + "\n";
        s += myStudents[myNumStudents-1];
        return s;
    }
}

and

import chn.util.*;
import java.util.Properties;
import java.util.Enumeration;
import java.util.*;

public class ArrayRecordsSortSearchApplication {

    private static final String STUDENT_FILENAME = "students25.txt";

    public static void main(String args[]) {
        Student[] sortedStudents = null;
        ClassPeriod p1 = new ClassPeriod("PERIOD 1");
        readClass(p1);
        ConsoleIO console = new ConsoleIO();
        char choice;

        do {
            showMenu();
            choice = console.readLine().charAt(0);
            System.out.println();
            switch (choice) {
            case '1':
                showStudents(p1.getStudents());
            case '2':
                break;
            default:
                System.out.println("That's not a choice");
                break;
            }
        } while (choice != '2');
    }

    public static void showMenu() {
        System.out.println();
        System.out.println("1)  Show students in original order");
        System.out.println("2)  Quit?");
        System.out.print("choice: ");
    }

    public static void showStudents(Student[] studs){
        System.out.print("studs.length: " +studs.length +"\n");
        for (int i=0; i<studs.length; i++)
           System.out.println(studs[i]); 
    }

    public static void readClass(ClassPeriod p1){
        System.out.println("Please wait while data file loads...");
        FileInput infile = new FileInput(STUDENT_FILENAME);

        do {
            int id = infile.readInt();
            double gpa = infile.readDouble();
            String name = infile.readLine();
            Student s = new Student(name,id,gpa);
            p1.addStudent(s);
        } while ( infile.hasMoreLines() );

        infile.close();
    }
}

Upvotes: 0

Views: 1117

Answers (1)

Milind Gokhale
Milind Gokhale

Reputation: 585

This assignment looks like the implementation of ArrayList. As soon as the size of the old array is exceeded, the new array needs to be created of double size and copy the elements of the old array into a new array and then add the new element to be inserted.

In your code:

if (myNumStudents == 10 || myNumStudents == 20) {//student array size is 10, if 10 is reached, double its size
                N = 2*myNumStudents;
                myStudents = new Student[N];
                ....somewhere here you should actually copy all the elements from the old array as first elements of the new array and then insert the new element to be inserted in array. 
}

Upvotes: 1

Related Questions