0

I am doing a homework assignment and I am almost finished with it. The last part here is to create a file, read from that file then the contents are used to perform some calculations. According to the API and a couple sites I looked at my syntax is correct but for some reason it is not performing as expected. Any help to steer me in the right direction would be appreciated. I am getting the exception errors for both reading and writing.

try{
        //In the space below (between Marker 2 and Marker 3) declare an 
        //ObjectOutputStream object called "outFile" for the purpose of 
        //writing Fraction objects into a file called "fraction.out"

        //Marker 2

         FileOutputStream fos = new FileOutputStream("fraction.out");
         ObjectOutputStream outFile = new ObjectOutputStream(fos);


        //Marker 3

        score += 5;

        outFile.writeObject(F[1]);
        outFile.writeObject(F[2]);
        outFile.writeObject(F[3]);
        outFile.close();
    }
    catch(Exception e){
        System.out.println("Could not write objects to file");
    }


    try{
        //In the space below (between Marker 4 and Marker 5) declare an 
        //ObjectInputStream object called "inFile" for the purpose of 
        //reading Fraction objects from a file called "fraction.out"

        //Marker 4

        ObjectInputStream inFile =
                 new ObjectInputStream(new FileInputStream("fraction.out"));


        //Marker 5

        score += 5;


        //In the space below (between Marker 6 and Marker 7) Complete
        //statements that read three fraction objects from the file 
        //as F[5], F[6], and F[7]

        //Marker 6

        f[5] = (Fraction) inFile.readObject();
        f[6] = (Fraction) inFile.readObject();
        f[7] = (Fraction) inFile.readObject();


        //Marker 7
        inFile.close();

        F[4] = F[5].multiply(F[6]).multiply(F[7]);
        score += 5;
        System.out.println("step 12:\tf4 = " + F[4]);
    }
    catch (Exception e){
        System.out.println("Could not read objects from file");

    }

1 Answer 1

1

The return value of readObject() is the object read from the stream. You are throwing it away.

2
  • Should I put something in arguments of readObject() so that it would work? Is that what you are telling me?
    – user2053184
    Nov 14, 2013 at 2:29
  • No he is saying to save the value from readObject() into a variable. e.g. MyObject myobj = readObject()
    – nook
    Nov 14, 2013 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.