Ok, I’m a bit closer but still getting a few errors. Netbeans is telling me my loanArray is not initialized and that it can’t find the readLine() method? Also, my attempt to close in my finally block is listing errors.
Here’s my code-
// generate 2 constants for use in the array lookup values.
final int YEARS = 0;
final int INTEREST = 1;
//create the array
double loanArray[][];
try{
FileReader readTerms = new FileReader ("MortgageTerms.txt");
BufferedReader loanTerms = new BufferedReader(readTerms);
java.util.Scanner termScan = new Scanner(loanTerms);
while(termScan.hasNext()){
for(int i=0;termScan != null; i++)
loanArray [i][YEARS]= Integer.parseInt (termScan.readLine());
loanArray [i][INTEREST] = Double.parseDouble (termScan.readLine());
}
} catch (FileNotFoundException e){
javax.swing.JOptionPane.showMessageDialog(null,
"Error, File not found");
return;
} catch (IOException ex){
javax.swing.JOptionPane.showMessageDialog(null, "There was an IO error");
return;
}
finally{
if (termScan!=null){
termScan.close();
}
}
Why don’t you believe the compiler? You need something like this:
And
termScanis declared inside the try block, so it’s out of scope by the time you get to finally. Declare it outside the try.This looks like a poor abstraction to me, one that fails to capitalize on objects.
Interest should be a double; years should be an int. Both should be encapsulated into a Terms or Loan object and stored in a List. Your matrix of doubles is a poor design choice.