2 Replies - 489 Views - Last Post: 22 August 2009 - 10:22 AM Rate Topic: -----

#1 tastysoup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-August 09

Array from a file

Posted 21 August 2009 - 09:49 PM

Hi all,

I am already having trouble with my homework this week. Can somebody check this out and tell my what I am doing wrong as far as populating the loanTerms[] array from the terms.txt file?

Also, the terms.txt file reads: 84,180,360

is that correct?

Thanks!






import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.math.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.text.*;
import javax.swing.*;
import javax.swing.JPanel;



public class GLC52 extends JFrame implements ActionListener

{

private Button keys[];
private JPanel keypad;

private JScrollPane scroller1;
private double op1;
private boolean first;
private boolean foundKey;
private boolean clearText;
private int lastOp;
private DecimalFormat calcPattern;
private DecimalFormat df = new DecimalFormat("$###,###.00");
private DecimalFormat currency = new DecimalFormat( "$###,##0.00" );
private DecimalFormat percent = new DecimalFormat( "###.000%" );
private DecimalFormat years = new DecimalFormat( "00 months" );
private JPanel content;


private int term;
private double rate;
private double loanAmt = 0.0;
private int []loanTerms = new int[3];
private double []intRates = {0.0535, 0.055, 0.0575};
JTextArea lcd = new JTextArea(20,52);
private double intrest = 0.0;
private int loanterm = 0;

private double payment = 0.0;










public double getTerms(loanTerms[])


{

BufferedReader rt = new BufferedReader (new FileReader ("terms.txt"));

for (int i = 0; i < 3; i++)
{


loanTerms [i] = Integer.parseInt(rt.readLine ());
}


return loanTerms[]

}












public GLC52()



	{





String message =
		"Welcome to Mortage Calculator!! \n\nInput loan amount by clicking the keypad, \nchoose loan term and interest rate from the menu \nand then click Enter";
		JOptionPane.showMessageDialog(null,message,"Mortgage Calculator", JOptionPane.
		INFORMATION_MESSAGE);









	//create instance of the menu
	MenuBar mnuBar = new MenuBar();
	setMenuBar(mnuBar);


	//construct and populate the File menu
	Menu mnuFile = new Menu("File", true);
	mnuBar.add(mnuFile);
		MenuItem mnuFileClear = new MenuItem("Clear");
		mnuFile.add(mnuFileClear);
		MenuItem mnuFileExit = new MenuItem("Exit");
		mnuFile.add(mnuFileExit);



	// construnct and populate About menu
	Menu mnuAbout = new Menu("About", true);
		mnuBar.add(mnuAbout);
		MenuItem mnuAboutCalculator = new MenuItem("About Calculator");
		mnuAbout.add(mnuAboutCalculator);





//construct and populate the Term menu
	Menu mnuTerm = new Menu("Term", true);
		mnuBar.add(mnuTerm);
		MenuItem mnuTermSeven = new MenuItem("7 Years");
		mnuTerm.add(mnuTermSeven);
		MenuItem mnuTermFifteen = new MenuItem("15 Years");
		mnuTerm.add(mnuTermFifteen);
		MenuItem mnuTermThirty = new MenuItem("30 Years");
		mnuTerm.add(mnuTermThirty);


//construct and populate the Interest Rate menu
	Menu mnuRate = new Menu("Interest Rate", true);
		mnuBar.add(mnuRate);
		MenuItem mnuRate535 = new MenuItem("5.35%");
		mnuRate.add(mnuRate535);
		MenuItem mnuRate550 = new MenuItem("5.50%");
		mnuRate.add(mnuRate550);
		MenuItem mnuRate575 = new MenuItem("5.75%");
		mnuRate.add(mnuRate575);






	// add the ActionListener to each menu item
	mnuFileClear.addActionListener(this);
	mnuFileExit.addActionListener(this);
	mnuAboutCalculator.addActionListener(this);
	mnuAboutCalculator.addActionListener(this);
	mnuTermSeven.addActionListener(this);
	mnuTermFifteen.addActionListener(this);
	mnuTermThirty.addActionListener(this);
	mnuRate535.addActionListener(this);
	mnuRate550.addActionListener(this);
	mnuRate575.addActionListener(this);


	// assign an ActionCommand to each menu item
	mnuFileClear.setActionCommand("Clear");
	mnuFileExit.setActionCommand("Exit");
	mnuAboutCalculator.setActionCommand("About");
	mnuRate535.setActionCommand("5.35%");
	mnuRate550.setActionCommand("5.50%");
	mnuRate575.setActionCommand("5.75%");

	// construct componenets and initalize begining values

	 //... Set textarea's initial, scrolling, and border.

			JScrollPane scroller1 = new JScrollPane(lcd);
			scroller1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

	lcd.setEditable(false);




	keypad = new JPanel();
	keys = new Button[11];
	first = true;
	op1 = 0.0;
	clearText = true;
	lastOp = 0;
	calcPattern = new DecimalFormat("########.########");


	// construct and assign captions to the Buttons
	for (int i=0; i<=9; i++)
		keys[i] = new Button(String.valueOf(i));

	keys[10] = new Button("Enter");


	// set Frame and keypad layout to grid layout

	keypad.setLayout(new GridLayout (4,4,5,5));

	for (int i=7; i<=9; i++) //7,8,9,
		keypad.add(keys[i]);

	for (int i=4; i<=6; i++) //4,5,6
		keypad.add(keys[i]);

	for (int i=1; i<=3; i++) // 1,2,3
		keypad.add(keys[i]);

	keypad.add(keys[0]); // 0 key
	keypad.add(keys[10]); // enter


	for (int i=0; i<keys.length; i++)
		keys[i].addActionListener(this);

	add(scroller1, BorderLayout.NORTH);
	add(keypad, BorderLayout.CENTER);




	addWindowListener(
		new WindowAdapter()
			{
			public void windowClosing(WindowEvent e)

				{
					System.exit(0);
				}
			}
		);



	} // end of constructor method




public void actionPerformed(ActionEvent e)
	{



	// test for menu item clicks
	String arg = e.getActionCommand();
	if (arg == "Exit")
		System.exit(0);

	if (arg == "Clear")
		{
		clearText = true;
		first = true;
		op1 = 0.0;
		lcd.setText("");
		lcd.requestFocus();
		}


	if (arg == "About")
		{
		String message =
		"Mortage Calculator by Titus Grupp. Week 2 University of Phoenix Java II";
		JOptionPane.showMessageDialog(null,message,"About Mortgage Calculator", JOptionPane.
		INFORMATION_MESSAGE);


		}

if (arg == "7 Years")
		{
		loanterm = loanTerms[0];

		}



if (arg == "15 Years")
		{
		loanterm = loanTerms[1];

		}


if (arg == "30 Years")
		{
		loanterm = loanTerms[2];

		}



if (arg == "5.35%")
		{
		intrest = intRates[0];

		}



if (arg == "5.50%")
		{
		intrest = intRates[1];

		}


if (arg == "5.75%")
		{
		intrest = intRates[2];

		}






	// test for button clicks
	foundKey = false;

	// search for the clicked key
	for (int i=0; i<keys.length && !foundKey; i++)
		{
		if(e.getSource() == keys[i])
			{
			foundKey=true;
			switch(i)
				{
				// number and decimal point buttons
				case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
				if(clearText)
					{
					lcd.setText("");
					clearText = false; // !clearTect;
					}
				lcd.setText(lcd.getText() + keys[i].getLabel());
				break;

				// operator buttons
				case 10: clearText = true;




				if  (first) // first operand
					{
					if(lcd.getText().length()==0) op1 = 0.0;
					else op1 = Double.parseDouble(lcd.getText());

					first = false;
					clearText = true;
					lastOp = i; // save last operator


					}

					double zeroChk = Double.parseDouble(lcd.getText()); //check for zero inputs

					if (zeroChk == 0.0)//check for zero inputs

					{
						String message =
								"Please enter a loan amount greater than 0.0";
								JOptionPane.showMessageDialog(null,message,"Incorrect input", JOptionPane.
		INFORMATION_MESSAGE);
					}



					else if (loanterm == 0.0)//check for zero inputs

																				{
																					String message =
																							"Please choose a loan term from the menu.";
																							JOptionPane.showMessageDialog(null,message,"Incorrect input", JOptionPane.
																	INFORMATION_MESSAGE);
										}

					else if (intrest == 0.0)//check for zero inputs

										{
											String message =
													"Please choose an interest rate from the menu.";
													JOptionPane.showMessageDialog(null,message,"Incorrect input", JOptionPane.
							INFORMATION_MESSAGE);
					}



					else //process input when Enter is clicked
					{

			payment =  formula1( loanAmt, intrest, loanterm );


			double yrs = loanterm/12;
			double anInt = zeroChk*intrest;
			double totInt = anInt*yrs;
			double repayTot = zeroChk+totInt;
			double intDif = repayTot-zeroChk;
			double curTot;
			double curInt;
			double monthInt;
			double stArt = repayTot-intDif;
			int counter = 0;





					lcd.setText("\t\tLoan amount " + currency.format( zeroChk ) + "\n\t\tLoan Term " + years.format( loanterm ) + "\n\t\tInterest rate " + percent.format( intrest ) + "\n\t\tMonthly Payment " + currency.format( payment ));





					lcd.append("\n\n\nPayment Number" + "\tRemaining Balance" + "\tInterest Paid");


			curTot = stArt;

			while (curTot > 0)

				{
					counter = counter + 1;
					curTot = curTot - payment;
					curInt = intrest/12;
					monthInt = curTot*curInt;
					lcd.append("\n" + counter + "\t\t" +  currency.format( curTot ) + "\t\t" + currency.format( monthInt));

				}









				}



					break;
				} // end switch(i)
			} // end if
		} // end for

	} // end actionPerformed







		public double formula1( double loanAmt, double rate, int term )  //loan calculation method
	{
		double mnthlyInt = rate / 12;
		int period = term * 12;
		loanAmt = Double.parseDouble(lcd.getText());

		double adjustment = (Math.pow((1 + mnthlyInt), term) -1) / 			(mnthlyInt * Math.pow((1 			+ mnthlyInt), term));





		loanAmt = loanAmt / adjustment; //calculate payable amount per month


		return loanAmt;


	}

































public static void main(String args[])
	{




	// set frame properties
		JFrame f = new GLC52();
		f.setTitle("Calculator Application");
		f.setBounds(200,200,600,600);
	f.setVisible(true);


	}//end main
}//end class




This post has been edited by tastysoup: 21 August 2009 - 09:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array from a file

#2 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Array from a file

Posted 21 August 2009 - 10:19 PM

you had a probleme with the getTerms void fct
try this code and let me know if it is helpful

public int[] getTerms(int[] loanTerms)
{
try
{
BufferedReader rt = new BufferedReader (new FileReader ("terms.txt"));
for (int i = 0; i < 3; i++)
{
loanTerms [i] = Integer.parseInt(rt.readLine ());
}
}catch(Exception e)
{	
}
return loanTerms;
}



dont forget to add those :
import java.io.*;
import java.util.*;
because without them the BufferedReader and FileReader are not recognized
and plz next time avoice the big spaces in your code :)

This post has been edited by oQMr FoxQo: 21 August 2009 - 10:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 tastysoup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-August 09

Re: Array from a file

Posted 22 August 2009 - 10:22 AM

Hi again,

Thanks! Sorr about the spaces, I forgot to take them out from when I was working on the code.

That fixed it but now I am having trouble calling on it. At the begining of m actionPerformed method, I am not sure how to call the loanTerms arra from the getTerms method. Is this not working because it is an array?


import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.math.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.text.*;
import javax.swing.*;
import javax.swing.JPanel;
import java.io.*;
import java.util.*;

public class GLC52 extends JFrame implements ActionListener
{
private Button keys[];
private JPanel keypad;
private JScrollPane scroller1;
private double op1;
private boolean first;
private boolean foundKey;
private boolean clearText;
private int lastOp;
private DecimalFormat calcPattern;
private DecimalFormat df = new DecimalFormat("$###,###.00");
private DecimalFormat currency = new DecimalFormat( "$###,##0.00" );
private DecimalFormat percent = new DecimalFormat( "###.000%" );
private DecimalFormat years = new DecimalFormat( "00 months" );
private JPanel content;
private int term;
private double rate;
private double loanAmt = 0.0;
private int []loanTerms = new int[3];
private double []intRates = {0.0535, 0.055, 0.0575};
JTextArea lcd = new JTextArea(20,52);
private double intrest = 0.0;
private int loanterm = 0;

private double payment = 0.0;

public int[] getTerms(int[] loanTerms)
{
try
{
BufferedReader rt = new BufferedReader (new FileReader ("terms.txt"));
for (int i = 0; i < 3; i++)
{
loanTerms [i] = Integer.parseInt(rt.readLine ());
}
}catch(Exception e)
{
}
return loanTerms;
}



public GLC52()
	{
String message =
		"Welcome to Mortage Calculator!! \n\nInput loan amount by clicking the keypad, \nchoose loan term and interest rate from the menu \nand then click Enter";
		JOptionPane.showMessageDialog(null,message,"Mortgage Calculator", JOptionPane.
		INFORMATION_MESSAGE);
//create instance of the menu
	MenuBar mnuBar = new MenuBar();
	setMenuBar(mnuBar);
//construct and populate the File menu
	Menu mnuFile = new Menu("File", true);
	mnuBar.add(mnuFile);
		MenuItem mnuFileClear = new MenuItem("Clear");
		mnuFile.add(mnuFileClear);
		MenuItem mnuFileExit = new MenuItem("Exit");
		mnuFile.add(mnuFileExit);
// construnct and populate About menu
	Menu mnuAbout = new Menu("About", true);
		mnuBar.add(mnuAbout);
		MenuItem mnuAboutCalculator = new MenuItem("About Calculator");
		mnuAbout.add(mnuAboutCalculator);
//construct and populate the Term menu
	Menu mnuTerm = new Menu("Term", true);
		mnuBar.add(mnuTerm);
		MenuItem mnuTermSeven = new MenuItem("7 Years");
		mnuTerm.add(mnuTermSeven);
		MenuItem mnuTermFifteen = new MenuItem("15 Years");
		mnuTerm.add(mnuTermFifteen);
		MenuItem mnuTermThirty = new MenuItem("30 Years");
		mnuTerm.add(mnuTermThirty);
//construct and populate the Interest Rate menu
	Menu mnuRate = new Menu("Interest Rate", true);
		mnuBar.add(mnuRate);
		MenuItem mnuRate535 = new MenuItem("5.35%");
		mnuRate.add(mnuRate535);
		MenuItem mnuRate550 = new MenuItem("5.50%");
		mnuRate.add(mnuRate550);
		MenuItem mnuRate575 = new MenuItem("5.75%");
		mnuRate.add(mnuRate575);
// add the ActionListener to each menu item
	mnuFileClear.addActionListener(this);
	mnuFileExit.addActionListener(this);
	mnuAboutCalculator.addActionListener(this);
	mnuAboutCalculator.addActionListener(this);
	mnuTermSeven.addActionListener(this);
	mnuTermFifteen.addActionListener(this);
	mnuTermThirty.addActionListener(this);
	mnuRate535.addActionListener(this);
	mnuRate550.addActionListener(this);
	mnuRate575.addActionListener(this);
// assign an ActionCommand to each menu item
	mnuFileClear.setActionCommand("Clear");
	mnuFileExit.setActionCommand("Exit");
	mnuAboutCalculator.setActionCommand("About");
	mnuRate535.setActionCommand("5.35%");
	mnuRate550.setActionCommand("5.50%");
	mnuRate575.setActionCommand("5.75%");
	// construct componenets and initalize begining values
//... Set textarea's initial, scrolling, and border.

	JScrollPane scroller1 = new JScrollPane(lcd);
	scroller1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

	lcd.setEditable(false);
	keypad = new JPanel();
	keys = new Button[11];
	first = true;
	op1 = 0.0;
	clearText = true;
	lastOp = 0;
	calcPattern = new DecimalFormat("########.########");
// construct and assign captions to the Buttons
	for (int i=0; i<=9; i++)
	keys[i] = new Button(String.valueOf(i));
	keys[10] = new Button("Enter");
// set Frame and keypad layout to grid layout
	keypad.setLayout(new GridLayout (4,4,5,5));

	for (int i=7; i<=9; i++) //7,8,9,
		keypad.add(keys[i]);

	for (int i=4; i<=6; i++) //4,5,6
		keypad.add(keys[i]);

	for (int i=1; i<=3; i++) // 1,2,3
	keypad.add(keys[i]);
	keypad.add(keys[0]); // 0 key
	keypad.add(keys[10]); // enter
	for (int i=0; i<keys.length; i++)
	keys[i].addActionListener(this);
	add(scroller1, BorderLayout.NORTH);
	add(keypad, BorderLayout.CENTER);
	addWindowListener(
		new WindowAdapter()
			{
			public void windowClosing(WindowEvent e)

				{
					System.exit(0);
				}
			}
		);
	} // end of constructor method


public void actionPerformed(ActionEvent e)
	{


		loanTerms[] = getTerms(loanTerms[]);



	// test for menu item clicks
	String arg = e.getActionCommand();
	if (arg == "Exit")
		System.exit(0);

	if (arg == "Clear")
		{
		clearText = true;
		first = true;
		op1 = 0.0;
		lcd.setText("");
		lcd.requestFocus();
		}

	if (arg == "About")
		{
		String message =
		"Mortage Calculator by Titus Grupp. Week 2 University of Phoenix Java II";
		JOptionPane.showMessageDialog(null,message,"About Mortgage Calculator", JOptionPane.
		INFORMATION_MESSAGE);

		}

if (arg == "7 Years")
		{
		loanterm = loanTerms[0];

		}

if (arg == "15 Years")
		{
		loanterm = loanTerms[1];
		}

if (arg == "30 Years")
		{
		loanterm = loanTerms[2];
		}

if (arg == "5.35%")
		{
		intrest = intRates[0];
		}
if (arg == "5.50%")
		{
		intrest = intRates[1];
		}
if (arg == "5.75%")
		{
		intrest = intRates[2];

		}
	// test for button clicks
	foundKey = false;

	// search for the clicked key
	for (int i=0; i<keys.length && !foundKey; i++)
		{
		if(e.getSource() == keys[i])
			{
			foundKey=true;
			switch(i)
				{
				// number and decimal point buttons
				case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
				if(clearText)
					{
					lcd.setText("");
					clearText = false; // !clearTect;
					}
				lcd.setText(lcd.getText() + keys[i].getLabel());
				break;

				// operator buttons
				case 10: clearText = true;

				if  (first) // first operand
					{
					if(lcd.getText().length()==0) op1 = 0.0;
					else op1 = Double.parseDouble(lcd.getText());

					first = false;
					clearText = true;
					lastOp = i; // save last operator

					}

					double zeroChk = Double.parseDouble(lcd.getText()); //check for zero inputs

					if (zeroChk == 0.0)//check for zero inputs

					{
						String message =
								"Please enter a loan amount greater than 0.0";
								JOptionPane.showMessageDialog(null,message,"Incorrect input", JOptionPane.
		INFORMATION_MESSAGE);
					}

					else if (loanterm == 0.0)//check for zero inputs
																				{
																					String message =
																							"Please choose a loan term from the menu.";
																							JOptionPane.showMessageDialog(null,message,"Incorrect input", JOptionPane.
																	INFORMATION_MESSAGE);
										}

					else if (intrest == 0.0)//check for zero inputs

										{
											String message =
													"Please choose an interest rate from the menu.";
													JOptionPane.showMessageDialog(null,message,"Incorrect input", JOptionPane.
							INFORMATION_MESSAGE);
					}


					else //process input when Enter is clicked
					{

			payment =  formula1( loanAmt, intrest, loanterm );

			double yrs = loanterm/12;
			double anInt = zeroChk*intrest;
			double totInt = anInt*yrs;
			double repayTot = zeroChk+totInt;
			double intDif = repayTot-zeroChk;
			double curTot;
			double curInt;
			double monthInt;
			double stArt = repayTot-intDif;
			int counter = 0;

					lcd.setText("\t\tLoan amount " + currency.format( zeroChk ) + "\n\t\tLoan Term " + years.format( loanterm ) + "\n\t\tInterest rate " + percent.format( intrest ) + "\n\t\tMonthly Payment " + currency.format( payment ));


					lcd.append("\n\n\nPayment Number" + "\tRemaining Balance" + "\tInterest Paid");

			curTot = stArt;

			while (curTot > 0)

				{
					counter = counter + 1;
					curTot = curTot - payment;
					curInt = intrest/12;
					monthInt = curTot*curInt;
					lcd.append("\n" + counter + "\t\t" +  currency.format( curTot ) + "\t\t" + currency.format( monthInt));

				}

				}

					break;
				} // end switch(i)
			} // end if
		} // end for

	} // end actionPerformed


		public double formula1( double loanAmt, double rate, int term )  //loan calculation method
	{
		double mnthlyInt = rate / 12;
		int period = term * 12;
		loanAmt = Double.parseDouble(lcd.getText());

		double adjustment = (Math.pow((1 + mnthlyInt), term) -1) / 			(mnthlyInt * Math.pow((1 			+ mnthlyInt), term));
		loanAmt = loanAmt / adjustment; //calculate payable amount per month

		return loanAmt;
	}

public static void main(String args[])
	{
	// set frame properties
		JFrame f = new GLC52();
		f.setTitle("Calculator Application");
		f.setBounds(200,200,600,600);
	f.setVisible(true);


	}//end main
}//end class














View PostoQMr FoxQo, on 21 Aug, 2009 - 09:19 PM, said:

you had a probleme with the getTerms void fct
try this code and let me know if it is helpful

public int[] getTerms(int[] loanTerms)
{
try
{
BufferedReader rt = new BufferedReader (new FileReader ("terms.txt"));
for (int i = 0; i < 3; i++)

{
loanTerms [i] = Integer.parseInt(rt.readLine ());
}
}catch(Exception e)
{	
}
return loanTerms;
}



dont forget to add those :
import java.io.*;
import java.util.*;
because without them the BufferedReader and FileReader are not recognized
and plz next time avoice the big spaces in your code :)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1