Hi
I am new to this forum and would appreciate any help that can be given. The following code is for an exercise in a book I am reading. It compiles fine but produces the following exception when executed.
java.io.InvalidClassException: Loan; local class incompatible: stream classdesc
serialVersionUID = -5273567861794013902, local class serialVersionUID = 19442595
53328340694
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unkno wn Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Exercise19_7.main(Exercise19_7.java:14)
Thanks in advance for any help.
Here is the code.
import java.io.*; import java.util.*; public class Exercise19_7 { public static void main(String[] args) throws ClassNotFoundException { ArrayList<Loan> loanList = new ArrayList<Loan>(); try{ ObjectInputStream input = new ObjectInputStream(new FileInputStream("Exercise19_6.dat")); while(true){ Loan loan = (Loan)(input.readObject()); loanList.add(loan); } } catch (EOFException ex) { for(int i = 0; i < loanList.size(); i++){ System.out.println("Loan Date: " + ((Loan)(loanList.get(i))).getLoanDate()); System.out.println("Loan Amount: " + ((Loan)(loanList.get(i))).getLoanAmount()); System.out.println("Interest Rate: " + ((Loan)(loanList.get(i))).getAnnualInterestRate()); System.out.println("Number of years: " + ((Loan)(loanList.get(i))).getNumberOfYears()); System.out.println("Total Payment: " + ((Loan)(loanList.get(i))).getTotalPayment()); } } catch (IOException ex) { ex.printStackTrace(); } } }
Here is the Loan class. It has worked fine in other exercises.
import java.util.Date; import java.io.*; public class Loan implements Serializable { private double annualInterestRate; private int numberOfYears; private double loanAmount; private Date loanDate; public Loan() { this(2.5, 1, 1000); } public Loan(double annualInterestRate, int numberOfYears, double loanAmount) throws IllegalArgumentException { if (annualInterestRate <= 0) throw new IllegalArgumentException("Interest rate must be more than zero"); if (numberOfYears <= 0) throw new IllegalArgumentException("Number of years must be more than zero"); if (loanAmount <= 0) throw new IllegalArgumentException("Loan Amount must be more than zero"); this.annualInterestRate = annualInterestRate; this.numberOfYears = numberOfYears; this.loanAmount = loanAmount; loanDate = new Date(); } public Loan(Loan loan){ this.annualInterestRate = loan.getAnnualInterestRate(); this.numberOfYears = (int)(loan.getNumberOfYears()); this.loanAmount = loan.getLoanAmount(); this.loanDate = loan.getLoanDate(); } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) throws IllegalArgumentException { if (annualInterestRate <= 0) throw new IllegalArgumentException("Interest rate must be more than zero"); this.annualInterestRate = annualInterestRate; } public double getNumberOfYears() { return numberOfYears; } public void setNumberOfYears(int numberOfYears) throws IllegalArgumentException { if (numberOfYears <= 0) throw new IllegalArgumentException("Number of years must be more than zero"); this.numberOfYears = numberOfYears; } public double getLoanAmount() { return loanAmount; } public void setLoanAmount(double loanAmount) throws IllegalArgumentException { if (loanAmount <= 0) throw new IllegalArgumentException("Loan Amount must be more than zero"); this.loanAmount = loanAmount; } public double getMonthlyPayment() { double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12))); return monthlyPayment; } public double getTotalPayment() { double totalPayment = getMonthlyPayment() * numberOfYears * 12; return totalPayment; } public Date getLoanDate() { return loanDate; } }