Can someone point me in the right directions. I have to build on an existing mortgage program. I have to pull interest rates in from an external txt file and have them populate my array. The array will then be used in a combobox pull down. I don't know which class to use. File, File inFile?? below is the code I have to work with.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;


public class joeweek4 extends JPanel{

	//global variable
	private JComboBox IntRates;
	private JLabel prinlb, rateLb, termLb, paymtLb;
	private JButton calBtn, resetBtn;
	private JTextField prinTxFld, rateTxFld, termTxFld, monthlyPayment;
	private TextArea payAmortTA;
	
	String currentInt;
	
	public joeweek4()
	{
		String[] comboItems={"5.35","5.50","5.75"};
			
		//creates labels
		prinlb = new JLabel("Principal Amount:");
		rateLb = new JLabel("Interest Rate:");
		termLb = new JLabel("Term in years:");
		paymtLb = new JLabel("Monthly Payment:");
		
		//creates textfields
		prinTxFld = new JTextField("",10);
		rateTxFld = new JTextField("",5);
		termTxFld = new JTextField("",5);
		monthlyPayment = new JTextField("",10);
				
		//combobox pulldown
		IntRates = new JComboBox(comboItems);
		IntRates.setEditable(true);
		payAmortTA = new TextArea(20,50);
		
		//create buttons
		calBtn = new JButton("Calculate");
		calBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{
				calculateMortgage();
			}
		});
		
		resetBtn = new JButton("Reset");
		resetBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{
				prinTxFld.setText("");
				rateTxFld.setText("");
				termTxFld.setText("");
				monthlyPayment.setText("");
				payAmortTA.setText ("");
			}
		});
		
		//add labels and fields
		add(prinlb);
		add(prinTxFld);
		add(rateLb);
		add(IntRates);
		add(rateTxFld);
		add(termLb);
		add(termTxFld);
		add(paymtLb);
		add(monthlyPayment);
		add(payAmortTA);
		add(calBtn);
		add(resetBtn);
	}
	
	public void calculateMortgage()
	{
	
		//string array to accept which terms to use
		rateTxFld.setText(IntRates.getSelectedItem().toString());
		double principle=Double.parseDouble (prinTxFld.getText());
		double dblrateTxFld=Double.parseDouble(rateTxFld.getText());
		
			if (dblrateTxFld == 5.35)
				termTxFld.setText("7");
		
			else if (dblrateTxFld == 5.50)
				termTxFld.setText("15");
		
			else if (dblrateTxFld == 5.75)
				termTxFld.setText("30");
		
		int inttermTxFld=Integer.parseInt(termTxFld.getText());
		
		// Formats the output of dollar figures
		DecimalFormat two_decimal=new DecimalFormat("$0,000.00");
		
		double Payment;
		double InterestPaid;
		double prinTxFldBal;
		double temp;
		int Months;
		double intMonthlyRate = dblrateTxFld/100;
		int MonthTerm = inttermTxFld * 12;
		Months=MonthTerm;
		
		//formulas
		Payment=(principle * (intMonthlyRate/12)) / (1-1/Math.pow((1+intMonthlyRate/12), MonthTerm));
		
		//converts monthly payment to decimal format
		monthlyPayment.setText("" + (two_decimal.format(Payment)));
		
		payAmortTA.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");
		
		for (int counter=0; counter < MonthTerm; counter++)
		{
			temp=(1-1/Math.pow((1+intMonthlyRate/12), Months));
			InterestPaid = Payment * temp;
			principle = (principle - Payment + InterestPaid);
			MonthTerm = MonthTerm --;
			Months--;
			
			payAmortTA.append((counter + 1) + "\t\t" + (two_decimal.format(Payment) + "\t\t" + (two_decimal.format(principle)
			+ "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));
			
		}
	
	}
	
	private static void createAndShowGui()
	{
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		joeweek4 calculator = new joeweek4();
		calculator.setOpaque(true);
		frame.setContentPane(calculator);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		javax.swing.SwingUtilities.invokeLater(
		new Runnable()
		{
			public void run()
			{
				createAndShowGui();
			}
		}
		
		);	
	}
}

What is the format of your text file ???
And by format mean how will the interest rates or whatever be arranged in the text file.

> I don't know which class to use.

Use the Scanner class to read the textual data in a line oriented manner. Create your own input processor class which operates on a given line and creates a class instance which represents the given business entity or a primitive depending on your file format. Add the returned entity to a collection. Rinse and repeat for each line.

From what I understand, I just need to have the three interest rates each on thier own line.

5.75
5.0
5.25

From what I understand, I just need to have the three interest rates each on thier own line.

5.75
5.0
5.25

Well if you had the contents formatted as:-
InterestRate1=5.75
InterestRate2=5.0
InterestRate3=5.25

Then you could have used the java.util.Properties class's load() method, all you would have to pass to it is a FileInputStream object on your file, and then you could access the different interest rates by using the corresponding Properties objects getProperty(String) method. ex: p.getProperty("InterestRate1")

But I guess this would be an overkill and ~s.o.s~'s method will best suite your purpose.

I appreciate all the advice everyone. I have been doing some research on the scanner methond. I am very new to Java and I am having trouble understanding alot of it.

I also read about FileReader and BufferedReader. Would that work as well? I sort of understand how to read the file, I am in the dark on being able to send the data to the array that I use in my program to populate the dropdown ComboBox.

try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
        
        }
        in.close();
    } catch (IOException e) {
    }

This is what i have so far for reading the file... am I on the right track? How would I put this into my array statement?

I have a good start (I think). I have the my File class created and set it to print to the console to test it. When I run the class outside of the entire program, it works fine, I can't get it to compile once I place it in my final program. Please help!!

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;


public class joeweek5 extends JPanel{

	//global variable
	private JComboBox IntRates;
	private JLabel prinlb, rateLb, termLb, paymtLb;
	private JButton calBtn, resetBtn;
	private JTextField prinTxFld, rateTxFld, termTxFld, monthlyPayment;
	private TextArea payAmortTA;
	
	String currentInt;
	
	public joeweek5()
	{
	private static void (String[] args) {
    File file = new File("C:\\prg421\\week5work\\rates1.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

      // this statement reads the line from the file and print it to
        // the console.
        System.out.println(dis.readLine());
      }

      // dispose all the resources after using them.
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
	}

		String[] comboItems={"5.35","5.50","5.75"};
			
		//creates labels
		prinlb = new JLabel("Principal Amount:");
		rateLb = new JLabel("Interest Rate:");
		termLb = new JLabel("Term in years:");
		paymtLb = new JLabel("Monthly Payment:");
		
		//creates textfields
		prinTxFld = new JTextField("",10);
		rateTxFld = new JTextField("",5);
		termTxFld = new JTextField("",5);
		monthlyPayment = new JTextField("",10);
				
		//combobox pulldown
		IntRates = new JComboBox(comboItems);
		IntRates.setEditable(true);
		payAmortTA = new TextArea(20,50);
		
		//create buttons
		calBtn = new JButton("Calculate");
		calBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{
				calculateMortgage();
			}
		});
		
		resetBtn = new JButton("Reset");
		resetBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{
				prinTxFld.setText("");
				rateTxFld.setText("");
				termTxFld.setText("");
				monthlyPayment.setText("");
				payAmortTA.setText ("");
			}
		});
		
		//add labels and fields
		add(prinlb);
		add(prinTxFld);
		add(rateLb);
		add(IntRates);
		add(rateTxFld);
		add(termLb);
		add(termTxFld);
		add(paymtLb);
		add(monthlyPayment);
		add(payAmortTA);
		add(calBtn);
		add(resetBtn);
	}
	
	public void calculateMortgage()
	{
	
		//string array to accept which terms to use
		rateTxFld.setText(IntRates.getSelectedItem().toString());
		double principle=Double.parseDouble (prinTxFld.getText());
		double dblrateTxFld=Double.parseDouble(rateTxFld.getText());
		
			if (dblrateTxFld == 5.35)
				termTxFld.setText("7");
		
			else if (dblrateTxFld == 5.50)
				termTxFld.setText("15");
		
			else if (dblrateTxFld == 5.75)
				termTxFld.setText("30");
		
		int inttermTxFld=Integer.parseInt(termTxFld.getText());
		
		// Formats the output of dollar figures
		DecimalFormat two_decimal=new DecimalFormat("$0,000.00");
		
		double Payment;
		double InterestPaid;
		double prinTxFldBal;
		double temp;
		int Months;
		double intMonthlyRate = dblrateTxFld/100;
		int MonthTerm = inttermTxFld * 12;
		Months=MonthTerm;
		
		//formulas
		Payment=(principle * (intMonthlyRate/12)) / (1-1/Math.pow((1+intMonthlyRate/12), MonthTerm));
		
		//converts monthly payment to decimal format
		monthlyPayment.setText("" + (two_decimal.format(Payment)));
		
		payAmortTA.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");
		
		for (int counter=0; counter < MonthTerm; counter++)
		{
			temp=(1-1/Math.pow((1+intMonthlyRate/12), Months));
			InterestPaid = Payment * temp;
			principle = (principle - Payment + InterestPaid);
			MonthTerm = MonthTerm --;
			Months--;
			
			payAmortTA.append((counter + 1) + "\t\t" + (two_decimal.format(Payment) + "\t\t" + (two_decimal.format(principle)
			+ "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));
			
		}
	
	}
	
	private static void createAndShowGui()
	{
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		joeweek5 calculator = new joeweek5();
		calculator.setOpaque(true);
		frame.setContentPane(calculator);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		javax.swing.SwingUtilities.invokeLater(
		new Runnable()
		{
			public void run()
			{
				createAndShowGui();
			}
		}
		
		);	
	}
}

I also need the final code to place the interest rates into the array for comboitems. I left the precoded there for reference.

This article has been dead for over six months. Start a new discussion instead.