I need some assistance with my calculator code. I have developed a fully funcitoning mortgage calculator up to this point. I have one small change that I want to do. Right now this is considered an applet and I want it to be considered an application.

It is my understanding that I need to move my opening from:

public class Mortgage extends JApplet {

to

public class Mortgage extends JFrame {

Doing so renders my calc inoperable. Any assistance is greatly appreciated.

Here is my simple code:

package Mortgage;

import javax.swing.*;
import java.awt.*;
import java.text.*;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.FileReader;


public class Mortgage extends JApplet {
	private JLabel amountLabel=new JLabel("Loan amount: ");
	private JTextField amount=new JTextField();
	private JLabel termLabel=new JLabel("Term: ");
	private JTextField term=new JTextField();
	private JLabel rateLabel=new JLabel("Interest rate: ");
	private JTextField rate=new JTextField();
	private JComboBox termList;
	private JLabel payLabel=new JLabel("Monthly Payment: ");
	private JLabel payment=new JLabel();
	private JButton calculate=new JButton("Calculate");
	private JButton clear=new JButton("Clear");
	private JButton exit=new JButton("Exit");
	private JTextArea paymentSchedule=new JTextArea();
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);
	private Container cp = getContentPane();

	private JPanel up=new JPanel();


    private int[] trmArray;
    double[] intrstArray;

     //reading from sequential file
     public void loadFile()
     {
           Reader fis;
           try
           {

                fis = new FileReader("data.txt");

                BufferedReader b = new BufferedReader( fis );

                String[] line = b.readLine(  ).split(",");

                //Fill in term values
                trmArray = new int[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     trmArray[ i ] = Integer.parseInt(line[i].trim());
                }

                //Fill in rate values
                line = b.readLine(  ).split(",");
                intrstArray = new double[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     intrstArray[ i ] = Double.parseDouble(line[i].trim());
                }

                b.close();
                fis.close();
           }

           catch ( Exception e1 )
           {
                e1.printStackTrace(  );
           }
     }

	public void init()
    {
		// Take care of termList
        String[] terms= new String[trmArray.length];
        for ( int i = 0; i < trmArray.length; i++ )
        {
            terms[i] = String.valueOf(trmArray[i]);
        }

        termList = new JComboBox(terms);
        termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				// the source of the event is the combo box
                int index = cb.getSelectedIndex();
        		String termYear = (String)cb.getSelectedItem();
        		term.setText(termYear);
        		rate.setText(intrstArray[index]+"");
			}
		});

		// Take care of three buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					// calculate the monthly payment
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double t=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+(r/100),t)*(r/100)/(Math.pow(1+(r/100),t)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");
					payment.setText(df.format(monthlyPayment));
					// calculate the detailed loan

                    double totalInterest = 0;
                    double balance=p;
					double interest=0;
                    double principal=0;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<t; i++) {
						month=i+1;
						interest=balance*(r/100);
						balance=balance+interest-monthlyPayment;
                        principal = monthlyPayment - interest;

						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t");
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");

                        totalInterest+=interest;
					}

                    paymentSchedule.setText(buffer.toString());



				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");

			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});


		JPanel upScreen=new JPanel();
		upScreen.setLayout(new GridLayout(5,2));
		upScreen.add(amountLabel); upScreen.add(amount);
		upScreen.add(termLabel); upScreen.add(term);
		upScreen.add(new Label()); upScreen.add(termList);
		upScreen.add(rateLabel); upScreen.add(rate);
		upScreen.add(payLabel); upScreen.add(payment);
		JPanel buttons=new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		buttons.add(calculate); buttons.add(clear); buttons.add(exit);
		//JPanel up=new JPanel();
		up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
		up.add(upScreen); up.add(buttons);
		cp.add(BorderLayout.NORTH, up);
		cp.add(BorderLayout.CENTER, schedulePane);
	}


	public static void main(String[] args) {
		Mortgage applet = new Mortgage();
		JFrame frame = new JFrame("Travis' Awesome Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.getContentPane().add(applet);
		frame.setSize(400, 800);
                  applet.loadFile();
		applet.init();
		applet.start();
		frame.setVisible(true);
	}
}

Recommended Answers

All 18 Replies

Doing so renders my calc inoperable.

Can you explain what you did and what happened? Copy and paste all error messages here.

Several differences between applets and apps:
Applets rely on the browser to hold and display its contents. Apps use a JFrame.
Applets have specific methods that a browser calls for different purposes.
An app has one method it must have for the java program to start it executing: main()

What does Google say about converting an applet to an application?

If I change my opening to:

public class Mortgage extends JFrame {

I get the following error, "cannot find symbol method start()". --- line 187

I assume this is because it is trying to start an applet.

I want to get away from the applet and move to an application. I want to use JFrame.

I modified my code to eliminate all applet references. The program will now build and run however, it appears blank now. I think I am getting closer but could still use some advise on how to populate my info in the JFrame.

import javax.swing.*;
import java.awt.*;
import java.text.*;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.FileReader;


public class Mortgage extends JFrame {
	private JLabel amountLabel=new JLabel("Loan amount: ");
	private JTextField amount=new JTextField();
	private JLabel termLabel=new JLabel("Term: ");
	private JTextField term=new JTextField();
	private JLabel rateLabel=new JLabel("Interest rate: ");
	private JTextField rate=new JTextField();
	private JComboBox termList;
	private JLabel payLabel=new JLabel("Monthly Payment: ");
	private JLabel payment=new JLabel();
	private JButton calculate=new JButton("Calculate");
	private JButton clear=new JButton("Clear");
	private JButton exit=new JButton("Exit");
	private JTextArea paymentSchedule=new JTextArea();
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);
	private Container cp = getContentPane();

	private JPanel up=new JPanel();


    private int[] trmArray;
    double[] intrstArray;

     //reading from sequential file
     public void loadFile()
     {
           Reader fis;
           try
           {

                fis = new FileReader("data.txt");

                BufferedReader b = new BufferedReader( fis );

                String[] line = b.readLine(  ).split(",");

                //Fill in term values
                trmArray = new int[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     trmArray[ i ] = Integer.parseInt(line[i].trim());
                }

                //Fill in rate values
                line = b.readLine(  ).split(",");
                intrstArray = new double[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     intrstArray[ i ] = Double.parseDouble(line[i].trim());
                }

                b.close();
                fis.close();
           }

           catch ( Exception e1 )
           {
                e1.printStackTrace(  );
           }
     }

	public void init()
    {
		// Take care of termList
        String[] terms= new String[trmArray.length];
        for ( int i = 0; i < trmArray.length; i++ )
        {
            terms[i] = String.valueOf(trmArray[i]);
        }

        termList = new JComboBox(terms);
        termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				// the source of the event is the combo box
                int index = cb.getSelectedIndex();
        		String termYear = (String)cb.getSelectedItem();
        		term.setText(termYear);
        		rate.setText(intrstArray[index]+"");
			}
		});

		// Take care of three buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					// calculate the monthly payment
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double t=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+(r/100),t)*(r/100)/(Math.pow(1+(r/100),t)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");
					payment.setText(df.format(monthlyPayment));
					// calculate the detailed loan

                    double totalInterest = 0;
                    double balance=p;
					double interest=0;
                    double principal=0;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<t; i++) {
						month=i+1;
						interest=balance*(r/100);
						balance=balance+interest-monthlyPayment;
                        principal = monthlyPayment - interest;

						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t");
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");

                        totalInterest+=interest;
					}

                    paymentSchedule.setText(buffer.toString());



				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");

			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});


		JPanel upScreen=new JPanel();
		upScreen.setLayout(new GridLayout(5,2));
		upScreen.add(amountLabel); upScreen.add(amount);
		upScreen.add(termLabel); upScreen.add(term);
		upScreen.add(new Label()); upScreen.add(termList);
		upScreen.add(rateLabel); upScreen.add(rate);
		upScreen.add(payLabel); upScreen.add(payment);
		JPanel buttons=new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		buttons.add(calculate); buttons.add(clear); buttons.add(exit);
		//JPanel up=new JPanel();
		up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
		up.add(upScreen); up.add(buttons);
		cp.add(BorderLayout.NORTH, up);
		cp.add(BorderLayout.CENTER, schedulePane);
	}


	public static void main(String[] args) {
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.getContentPane();
		frame.setSize(400, 800);
		frame.setVisible(true);
	}
}

Any thoughts on how to get my info to appear in my JFrame?

You need to add some component(s) to the JFrame extended class for them to appear.
I don't see where you do that.
That could be done in the constructor for the Mortgage class which you could add to the class.

You have methods in the code that are never called.

Are you saying to do something like this:

public static void main(String[] args) {
5.			JFrame frame = new JFrame("Mortgage Calculator");
6.			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
7.			frame.setResizable(false);
8.			frame.getContentPane();
9.			frame.setSize(400, 800);
10.			frame.setVisible(true);
11.			JPanel upScreen=new JPanel();
12.			upScreen.setLayout(new GridLayout(5,2));
13.			upScreen.add(amountLabel); upScreen.add(amount);
14.			upScreen.add(termLabel); upScreen.add(term);
15.			upScreen.add(new Label()); upScreen.add(termList);
16.			upScreen.add(rateLabel); upScreen.add(rate);
17.			upScreen.add(payLabel); upScreen.add(payment);
18.			JPanel buttons=new JPanel();
19.			buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
20.			buttons.add(calculate); buttons.add(clear);
		         buttons.add(exit);
21.			up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
22.			up.add(upScreen); up.add(buttons);
23.			cp.add(BorderLayout.NORTH, up);
24.			cp.add(BorderLayout.CENTER, schedulePane);

Maybe I am just confused.

It seems like I am missing something small if my program will compile and run without error but my info will just not display.

You need to add components to frame before you call setVisible(true)
I don't see where you add anything to frame.

Many programs will compile and not run correctly. Its very easy to create that kind of program.

I guess I was hoping for a clearer explanation.

You need to add components to frame before you call setVisible(true)

What don't you understand about this statement?
frame is a variable refering to a JFrame object. Its a Container.
The add() method is used to add components to a container.
Your code does NOT add any components to the JFrame. So the JFrame has nothing to show.

I am looking for HELP on what I would put in the JFrame to make my info appear.

public static void main(String[] args) {		JFrame frame = new JFrame("Mortgage Calculator");		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		frame.setResizable(false);		frame.getContentPane();		frame.setSize(400, 800);		frame.setVisible(true);	}

I thought that this was a HELP forum. If you look at my first block of code, my initial code works great but it is an applet. I have run into difficulty trying to move it to an applicaiton vs an applet. I have spent the past 4 weeks putting that file together and I am proud of the work that I have accomplished.

I just need an example to get me started

There is a difference between my helping you fix your code and my changing your code.
I'm trying to help you change your code. I won't be writing code for you.

Where do you add any components to the JFrame class? You need to!

Put this code in your program just before the setVisible(true):

frame.add(new JLabel("This is a label"));

and see what happens

Are all your methods being called? Add println("hi from method METHODNAME HERE") statements to each method to verify which are being called and which are not.

Okay - that helped. Thank you. I added in the add() and it shows a label in what was just a grey box. So I went ahead and tried to add in one of the labels listed in my program:

frame.add(add(termLabel));

I get an error:

non-static method add(java.awt.Component) cannot be referenced from a static context.

I honestly have no idea what this error means.

Here is my updated code - thoughts?

import javax.swing.*;
import java.awt.*;
import java.text.*;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.FileReader;


public class Mortgaqge extends JFrame {
	private JLabel amountLabel=new JLabel("Loan amount: ");
	private JTextField amount=new JTextField();
	private JLabel termLabel=new JLabel("Term: ");
	private JTextField term=new JTextField();
	private JLabel rateLabel=new JLabel("Interest rate: ");
	private JTextField rate=new JTextField();
	private JComboBox termList;
	private JLabel payLabel=new JLabel("Monthly Payment: ");
	private JLabel payment=new JLabel();
	private JButton calculate=new JButton("Calculate");
	private JButton clear=new JButton("Clear");
	private JButton exit=new JButton("Exit");
	private JTextArea paymentSchedule=new JTextArea();
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);
	private Container cp = getContentPane();

	private JPanel up=new JPanel();


    private int[] trmArray;
    double[] intrstArray;

     //reading from sequential file
     public void loadFile()
     {
           Reader fis;
           try
           {

                fis = new FileReader("data.txt");

                BufferedReader b = new BufferedReader( fis );

                String[] line = b.readLine(  ).split(",");

                //Fill in term values
                trmArray = new int[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     trmArray[ i ] = Integer.parseInt(line[i].trim());
                }

                //Fill in rate values
                line = b.readLine(  ).split(",");
                intrstArray = new double[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     intrstArray[ i ] = Double.parseDouble(line[i].trim());
                }

                b.close();
                fis.close();
           }

           catch ( Exception e1 )
           {
                e1.printStackTrace(  );
           }
     }

	public void init()
    {
		// Take care of termList
        String[] terms= new String[trmArray.length];
        for ( int i = 0; i < trmArray.length; i++ )
        {
            terms[i] = String.valueOf(trmArray[i]);
        }

        termList = new JComboBox(terms);
        termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				// the source of the event is the combo box
                int index = cb.getSelectedIndex();
        		String termYear = (String)cb.getSelectedItem();
        		term.setText(termYear);
        		rate.setText(intrstArray[index]+"");
			}
		});

		// Take care of three buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					// calculate the monthly payment
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double t=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+(r/100),t)*(r/100)/(Math.pow(1+(r/100),t)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");
					payment.setText(df.format(monthlyPayment));
					// calculate the detailed loan

                    double totalInterest = 0;
                    double balance=p;
					double interest=0;
                    double principal=0;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<t; i++) {
						month=i+1;
						interest=balance*(r/100);
						balance=balance+interest-monthlyPayment;
                        principal = monthlyPayment - interest;

						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t");
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");

                        totalInterest+=interest;
					}

                    paymentSchedule.setText(buffer.toString());



				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");

			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});



	}


	public static void main(String[] args) {
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setResizable(false);
		frame.getContentPane();
		frame.setSize(400, 800);
		frame.add(add(termLabel));
		frame.setVisible(true);
	}
}

The error is because the variable termLabel only exists when there is a Mortgage object. You haven't created one yet. If you did, then you'd need the pointer to the created object to reference the variable: refToMort.termLabel


It's better to create the GUI in the class's constructor vs in the static main() method.
Move the GUI code out of the main() to the constructor.

Just have the one statement: new Mortgage(); // create and start app
in main();

Thank you for the help.

Here is where i am now.

I want my calculator to import from a text file. I have two text files. One has data in a horizontal format and the other is in more of a vertical alignment.

Here is my calculator code:

import javax.swing.*;
import java.awt.*;
import java.text.*;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.FileReader;


public class Mortgage extends JFrame {

	private JLabel amountLabel=new JLabel("Loan amount: ");
	private JTextField amount=new JTextField();
	private JLabel termLabel=new JLabel("Term: ");
	private JTextField term=new JTextField();
	private JLabel rateLabel=new JLabel("Interest rate: ");
	private JTextField rate=new JTextField();
	private JComboBox termList;
	private JLabel payLabel=new JLabel("Monthly Payment: ");
	private JLabel payment=new JLabel();
	private JButton calculate=new JButton("Calculate");
	private JButton clear=new JButton("Clear");
	private JButton exit=new JButton("Exit");
	private JTextArea paymentSchedule=new JTextArea();
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);
	private Container cp = getContentPane();

	private JPanel up=new JPanel();


    private int[] trmArray;
    double[] intrstArray;

     //reading from sequential file
     public Mortgage()
     {

           Reader fis;
           try
           {

                fis = new FileReader("loandata.txt");

                BufferedReader b = new BufferedReader( fis );

                String[] line = b.readLine(  ).split(",");

                //Fill in term values
                trmArray = new int[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     trmArray[ i ] = Integer.parseInt(line[i].trim());
                }

                //Fill in rate values
                line = b.readLine(  ).split(",");
                intrstArray = new double[line.length];
                for ( int i = 0; i < line.length; i++ )
                {
                     intrstArray[ i ] = Double.parseDouble(line[i].trim());
                }

                b.close();
                fis.close();
           }

           catch ( Exception e1 )
           {
                e1.printStackTrace(  );
           }
           init();

     }

	public void init()
    {
		// Take care of termList
        String[] terms= new String[trmArray.length];
        for ( int i = 0; i < trmArray.length; i++ )
        {
            terms[i] = String.valueOf(trmArray[i]);
        }

        termList = new JComboBox(terms);
        termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				// the source of the event is the combo box
                int index = cb.getSelectedIndex();
        		String termYear = (String)cb.getSelectedItem();
        		term.setText(termYear);
        		rate.setText(intrstArray[index]+"");
			}
		});

		// Take care of three buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					// calculate the monthly payment
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double n=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+r,n)*r/(Math.pow(1+r,n)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");
					payment.setText(df.format(monthlyPayment));
					// calculate the detailed loan

                    double totalInterest = 0;
                    double balance=p;
					double interest=0;
                    double principal=0;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<n; i++) {
						month=i+1;
						interest=balance*r;
						balance=balance+interest-monthlyPayment;
                        principal = monthlyPayment - interest;

						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t");
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");

                        totalInterest+=interest;
					}

                    paymentSchedule.setText(buffer.toString());




				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");

			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});


		JPanel upScreen=new JPanel();
		upScreen.setLayout(new GridLayout(5,2));
		upScreen.add(amountLabel); upScreen.add(amount);
		upScreen.add(termLabel); upScreen.add(term);
		upScreen.add(new Label()); upScreen.add(termList);
		upScreen.add(rateLabel); upScreen.add(rate);
		upScreen.add(payLabel); upScreen.add(payment);
		JPanel buttons=new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		buttons.add(calculate); buttons.add(clear); buttons.add(exit);
		//JPanel up=new JPanel();
		up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
		up.add(upScreen); up.add(buttons);
		cp.add(BorderLayout.NORTH, up);
		cp.add(BorderLayout.CENTER, schedulePane);
	}


	public static void main(String[] args) {
		JFrame frame = new Mortgage();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.getContentPane();
		frame.setSize(400, 800);
		frame.setVisible(true);
	}
}

I have two .txt files. As it is, the code imports my data.txt file. The data in this file is formated as such:

7,15,30,40
0.0535,0.055,0.0575,0.06


I want to convert my code to pull data from loandata.txt instead.

Any ideas on how to get started on this?

Here is the data format in my loandata.txt file.

7,5.35
10,5.0
15,5.75
30,6.5

Open the file and read the lines.
Use the String split() method to break the lines at the field separator char (,)
Pull the data out of the array created by split()

I only had 2 array files in my data.txt file.

With this new file, I would have 4 arrays, right?

I had each array named. Do I now need to name the 4 arrays before I can pull data from them?

This was easier in my other file as the data types were the same in each array. The new arrays have mixed data types.

Would you be able to provide an example? Not asking you to code for me, just asking for an example that I can coorelate back to my program.

The arrays I talk about are temporary and only exist while you are parsing a line with split() that is read in from the file. The data should be converted to a numberic and saved somewhere in your code. It appears the first number is an int and the second is a double.
Use the Integer.parseInt() to convert the first and Double.parseDouble() for the second.

okay - I am getting closer.

Here is where I am thus far:

import java.io.*;
import java.util.*;

public class Calc {
    public static void main(String[] args) throws Exception {
        String s = "7,5.35\n15,5.5\n30,5.75";
        BufferedReader in = new BufferedReader(new StringReader(s));
        String line = null;
        List<Loan> loans = new ArrayList<Loan>();
        while ((line = in.readLine()) != null) {
            String[] data = line.split(",");
            int term = Integer.parseInt(data[0]);
            double rate = Double.parseDouble(data[1]);
            Loan loan = new Loan(term, rate);
            loans.add(loan);
        }

I can get it to pull from a string of data. My challenge is to get this to pull from the external file. I need to remove the string and add in the ability to read from the external file.

Can you offer any assistance? Am I on the right path or have I stepped backward? I have one week to get this done and I am stressed a bit. LOL

Travis

I need to remove the string and add in the ability to read from the external file.

To read from a file, replace the StringReader with a FileReader.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.