Home > Mobile >  Why is my object array not being made correctly?
Why is my object array not being made correctly?

Time:02-28

Im trying to make an object array for my TestScores class which takes in an array and then can use the averageScore method to get the average score. However it seems like it only writes the last value of the object array as the output simply prints the last value that was inputted. I've tried using a system print line to get the object array output while in the for loop that populates it, which seems to work fine, but outside the loop it doesn't seem to give the expected output, which would be the average of each array inputted into the class.

import java.io.*;
import java.util.Scanner;
public class TestScores implements Serializable{

private int[] testArray;

public TestScores(int[] parameter)
        throws InvalidTestScore {
    testArray = parameter;

    for (int k : testArray) {
        if (k > 100 || k < 0) {
            throw new InvalidTestScore(k);
        }
    }
}

    public int averageScore() {
        int sum = 0;

        for (int j : testArray) {
            sum  = j;
        }
        return sum / testArray.length;
    }


public static void main(String[] args) throws InvalidTestScore, IOException, ClassNotFoundException {
    int input;
    int[] array = new int[2];

    Scanner keyboard = new Scanner(System.in);


    TestScores[] testScores = new TestScores[5];
    for(int k = 0; k < testScores.length; k  ) {

        for (int i = 0; i < 2; i  ) {
            System.out.print("Enter a score: ");
            input = keyboard.nextInt();
            array[i] = input;
        }
        System.out.println("Array created ");
        testScores[k] = new TestScores(array);
    }

    FileOutputStream outStream =
            new FileOutputStream("Object.dat");
    ObjectOutputStream objectOutputFile =
            new ObjectOutputStream(outStream);

    for (TestScores testScore : testScores) {
        objectOutputFile.writeObject(testScore);
    }
    objectOutputFile.close();


    System.out.println("Objects serialized and written to objects.dat");

    FileInputStream inStream =
            new FileInputStream("Object.dat");
    ObjectInputStream objectInputFile =
            new ObjectInputStream(inStream);

    TestScores[] scores2 =
            new TestScores[5];

    for(int m = 0; m < scores2.length; m  ){
        scores2[m] =
                (TestScores)objectInputFile.readObject();
    }

    objectInputFile.close();

    for(int n = 0; n < 5; n  ){
        System.out.println("Average score "   scores2[n].averageScore());
    }
}

}

As per the comments, I changed my code and instead made it an array of arrays, so I wasn't using the same array for the entire program.

    public static void main(String[] args) throws InvalidTestScore, 
    IOException, ClassNotFoundException {
    int input;
    int[][] array = new int[5][2];


    Scanner keyboard = new Scanner(System.in);


    TestScores[] testScores = new TestScores[5];
    for(int k = 0; k < testScores.length; k  ) {

        for (int i = 0; i < 2; i  ) {
            System.out.print("Enter a score: ");
            input = keyboard.nextInt();
            array[k][i] = input;
        }

        System.out.println("Array created ");
        testScores[k] = new TestScores(array[k]);
    }

CodePudding user response:

You add the same array of input data to all the TestScore objects. So if you modify array for subsequent instances of TestScore instances, you also modify all previously created instances.

There are two possible solutions that appear useful to me, pick the one that suits better for you:

  1. You write each TestScore instance immediately to the file within the first loop - instead of creating a new instance of TestScore! You don't need to have an array of TestScore for inputting all the values.
  2. You also create a new instance of array in the input before you create a new instance of TestSuite. So each TestSuite has it's own instance of array.
  • Related