Join 150,121 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,006 people online right now. Registration is fast and FREE... Join Now!
Hey guys, I stumbled upon this wonderful site while looking for a solution to my problem with my Java 2 assignment. I hope you guys can help.
This is my Mortgage calculator that prints out a chart showing the results. I got some help from other classmates on this and was able to get it to compile with no problems but it will not run at all. Any Ideas? I don't have much experience with java and have not came across a situation where my code would compile but not actually run so I'm completely lost here. Here is my code.
//reading from sequential file public void load() { Reader fis; try { fis = new InputStreamReader(getClass().getResourceAsStream("data.txt"));
BufferedReader b = new BufferedReader( fis );
String[] line = b.readLine( ).split(","); trmArray = new int[line.length]; for ( int i = 0; i < line.length; i++ ) { trmArray[ i ] = Integer.parseInt(line[i].trim()); }
line = b.readLine( ).split(","); intrstArray = new double[line.length]; for ( int i = 0; i < line.length; i++ ) { intrstArray[ i ] = Double.parseDouble(line[i].trim()); }
Llabel = new JLabel("Mortgage Loan Amount $ (no comma)");//amount label con.add(Llabel);
Ltextfield = new JTextField("");//amount textfield con.add(Ltextfield);
Olabel = new JLabel("Preset Term & Interest Rate %");//option label con.add(Olabel);
options = new JComboBox();//option combobox con.add(options);
MessageFormat mf = new MessageFormat("{0} years at {1,number,#.##}%"); options.addItem(" (choose rate)"); for (int i = 0; i < trmArray.length; i++) { options.addItem(mf.format(new Object[] { new Integer(trmArray[i]), new Double(intrstArray[i])})); }
Tlabel = new JLabel("Term (years)");//term label con.add(Tlabel);
Ttextfield = new JTextField("");//term textfield con.add(Ttextfield);
Rlabel = new JLabel("Interest Rate");//rate label con.add(Rlabel);
Rtextfield = new JTextField("");//rate textfield con.add(Rtextfield);
Plabel = new JLabel("Monthly Payment Amount");//payment label con.add(Plabel);
$label = new JLabel("");//payment textfield con.add($label);
calculate = new JButton("Calculate");//calculate button con.add(calculate);
reset = new JButton("Reset");//reset button con.add(reset);
exit = new JButton ("Exit");//exit button con.add(exit);
//create table and table model model = new DefaultTableModel(columnNames, 0); table = new JTable(model); JScrollPane scroll = new JScrollPane(table); table.setPreferredScrollableViewportSize(new Dimension (10, 600)); con.add (scroll);
graph = new JButton ("Display Graph");//Display Graph button con.add(graph);
//action event from listeners public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == calculate) { startCalculations(); }
if (source == reset) { reset(); }
if (source==options) { setRate(); }
if (source == exit) { exit(); }
if (source == mnuExit) { mFrame.dispose(); mFrame = null; } if (source == graph) { //creates new JFrame for graph mFrame = new JFrame("Mortgage Graph"); mFrame.getContentPane().add(new GraphPanel(yearlyPrinciple, yearlyInterest)); mFrame.setSize(800,600); mFrame.setLocation(200,100);
//creates menu bar // Create an instance of the menu (Creates the Menu Bar) JMenuBar mnuBar = new JMenuBar(); mFrame.setJMenuBar(mnuBar);
// Construct and populate the Exit menu (Creates the Exit Menu) JMenu mnuExitbar = new JMenu ("Exit", true); mnuBar.add(mnuExitbar); mnuExitbar.add(mnuExit);
//exit the program with thank you message void exit() { JOptionPane.showMessageDialog(null, " Thank you for using \n The Mortagae Calculator", "Message Dialog", JOptionPane.PLAIN_MESSAGE);
System.exit(0); }
public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Mortgage5().setVisible(true); } }); } }
//creates class for container layout and placement class Mortgage1Layout implements LayoutManager { public Mortgage1Layout() {}
public void addLayoutComponent(String name, Component comp) {}
public void removeLayoutComponent(Component comp) {}
public Dimension preferredLayoutSize(Container parent) { Dimension dim = new Dimension(0, 0); Insets insets = parent.getInsets(); dim.width = 600 + insets.left + insets.right; dim.height = 425 + insets.top + insets.bottom;
return dim; }
public Dimension minimumLayoutSize(Container parent) { Dimension dim = new Dimension(0, 0);
return dim; }
public void layoutContainer(Container parent) { Insets insets = parent.getInsets();
Component c; c = parent.getComponent(0); if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,250,24);} c = parent.getComponent(1); if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,175,24);} c = parent.getComponent(2); if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,250,24);} c = parent.getComponent(3); if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+40,150,24);} c = parent.getComponent(4); if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,250,24);} c = parent.getComponent(5); if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+72,96,24);} c = parent.getComponent(6); if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,250,24);} c = parent.getComponent(7); if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+104,112,24);} c = parent.getComponent(8); if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,250,24);} c = parent.getComponent(9); if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+136,112,24);} c = parent.getComponent(10); if (c.isVisible()) {c.setBounds(insets.left+50,insets.top+168,96,24);} c = parent.getComponent(11); if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+168,112,24);} c = parent.getComponent(12); if (c.isVisible()) {c.setBounds(insets.left+400,insets.top+168,96,24);} c = parent.getComponent(13); if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,575,160);} c = parent.getComponent(14); if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+375,112,24);} } }
//creates class for graphpanel class GraphPanel extends JPanel { final int HPAD = 60, VPAD = 40; int[] data; Font font; float[] principleData; float[] interestData;
public GraphPanel(float[] p, float[] i) {
principleData = p; interestData = i;
font = new Font("lucida sans regular", Font.PLAIN, 16); setBackground(Color.white); }
private int[] getDataVals() { int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; int j = interestData.length -1; max = (int)principleData[j]; min = (int)interestData[j]; int span = max - min; return new int[] { min, max, span }; } }
This post has been edited by Evolution: 16 Jun, 2008 - 08:19 AM
This is my Mortgage calculator that prints out a chart showing the results. I got some help from other classmates on this and was able to get it to compile with no problems but it will not run at all. Any Ideas? I don't have much experience with java and have not came across a situation where my code would compile but not actually run so I'm completely lost here.
Also pbl is right there is no problem with the imported packages. That was just the first think that came in my mind when I a saw all those chunk of code, speaking of:
CODE
import java.awt.*; /* and then importing individual classes ?? not necessary Also */ import javax.swing.*; import javax.CLASS_NAME; // again no need
1. It runs and compiles but when I press the button nothing happens. 2. It runs and compiles but the data output is not what I'm expecting. 3. It runs and compiles but the total is not correct.
well something like that. Be more informative if you may.