Needed some help with my logic again. Assuming the Loan class exists, I have a file containing Loan objects. I am reading the Loan objects from the file and calculating the total Loan amount. I gave one of the instances a variable name ld just to prove to myself that getLoanAmount() method works.
Now at the end in my while loop, I kept thinking on how to calculate the loan objects but turn them into integers. I’m thinking that the input.readObject() method is returning an instance of the object, so it would only make sense to go about getting the integer value like this:
sum = sum + input.readObject().getLoanAmount();
But, I’m getting an error saying that it can’t find the symbol getLoanAmount() and that’s where I’m puzzled. I thought I nailed it. Can someone tell me why my logic is wrong. As usual I would really appreciate it. Here’s the code:
import java.io.*;
import java.util.Date;
public class RestoringObjects19_7 {
public static void main(String[] args) throws IOException {
int sum = 0;
try {
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream("Exercise19_7.dat"));
Loan ld = new Loan(1000);
System.out.println(ld.getLoanAmount()); //WORKS HERE
output.writeObject(ld);
output.writeObject(new Loan(2000));
output.writeObject(new Loan(3000));
output.writeObject(new Loan(4000));
output.writeObject(new Loan(5000));
output.close();
ObjectInputStream input = new ObjectInputStream(
new FileInputStream("Exercise19.7.dat"));
while(true) {
sum = sum + input.readObject().getLoanAmount(); //ERROR HERE??
}
} catch (Exception e) {
System.out.println("Sum: " + sum);
}
}
}
This looks like it’s homework, so I’ll give you hints instead of a full blown answer:
input.readObject()returns type isObject.getLoanAmount()is a method that is defined on objects of typeLoan.Hope this helps you figure out what might be wrong with your code.