pink16
asked on
Java error
I receive this error when running my program. Its a Java Mortgage calculator For school. The assignment is:
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans. Use an array for the mortgage data for the different loans. Read the interest rates to fill the array from a sequential file. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Add graphics in the form of a chart. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program.
The program compiles fine and executes, but when it runs I receive this in the command prompt. Also I am unable to pull loan info from my external file. My external file has this
7, 0.0535
15, 0.0550
30, 0.0575
The error in the command prompt is this:
java.lang.NumberFormatExce ption: For input string: "0.0535"
at java.lang.NumberFormatExce ption.forI nputString (Unknown Source)
at java.lang.Integer.parseInt (Unknown Source)
at java.lang.Integer.parseInt (Unknown Source)
at Mort6.load(Mort6.java:94)
at Mort6.<init>(Mort6.java:72 )
at Mort6$1.run(Mort6.java:425 )
at java.awt.event.InvocationE vent.dispa tch(Unknow n Source)
at java.awt.EventQueue.dispat chEvent(Un known Source)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Unkn own Source)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(Unknown Source)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Unkn own Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.run(Unk nown Source)
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans. Use an array for the mortgage data for the different loans. Read the interest rates to fill the array from a sequential file. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Add graphics in the form of a chart. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program.
The program compiles fine and executes, but when it runs I receive this in the command prompt. Also I am unable to pull loan info from my external file. My external file has this
7, 0.0535
15, 0.0550
30, 0.0575
The error in the command prompt is this:
java.lang.NumberFormatExce
at java.lang.NumberFormatExce
at java.lang.Integer.parseInt
at java.lang.Integer.parseInt
at Mort6.load(Mort6.java:94)
at Mort6.<init>(Mort6.java:72
at Mort6$1.run(Mort6.java:425
at java.awt.event.InvocationE
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
/*
PRG 421 Java Programming II Change Request 7, Week 5
Programmer: Nate Himley
Date: May 9, 2009
Filename: Mort6.java
Purpose: Write the program in Java (with a graphical user interface) and have it calculate and
display the mortgage payment amount from user input of the amount of the mortgage and the user's
selection from a menu of available mortgage loans. Use an array for the mortgage data for the
different loans. Read the interest rates to fill the array from a sequential file. Display the
mortgage payment amount followed by the loan balance and interest paid for each payment over the
term of the loan. Add graphics in the form of a chart. Allow the user to loop back and enter a new
amount and make a new selection or quit. Please insert comments in the program to document the
program.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.font.*;
import java.awt.Font;
import java.awt.geom.*;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.io.FileReader;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Mort6 extends JFrame implements ActionListener
{
JLabel Llabel;
JTextField Ltextfield;
JLabel Olabel;
JComboBox options;
JLabel Tlabel;
JTextField Ttextfield;
JLabel Rlabel;
JTextField Rtextfield;
JLabel Plabel;
JLabel $label;
JButton calculate;
JButton reset;
JButton end;
JTable table;
JMenuItem mnuExit = new JMenuItem("Exit");
DefaultTableModel model;
int[] trmArray;
double[] intrstArray;
JButton graph;
private float[] yearlyPrinciple;
private float[] yearlyInterest;
// Title
public Mort6 ()
{
super("Nate Himley Week 5");
setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
load();
init();
pack();
setVisible(true);
}
//Pull info from file
public void load()
{
Reader fis;
try
{
fis = new FileReader("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());
}
b.close();
fis.close();
}
catch ( Exception e1 )
{
e1.printStackTrace( );
}
}
//labels, buttons and textfields
public void init()
{
Mort6Layout customLayout = new Mort6Layout();
Container con = getContentPane();
con.setLayout(customLayout);
con.setFont(new Font("Arial", Font.PLAIN, 12));
con.setLayout(customLayout);
Llabel = new JLabel("Mortgage Loan Amount $ (no comma)");
con.add(Llabel);
Ltextfield = new JTextField("");//amount textfield
con.add(Ltextfield);
Olabel = new JLabel("Term & Interest Rate %");
con.add(Olabel);
options = new JComboBox();
con.add(options);
options.addItem(" (Preset rate)");
options.addItem("7 years at 5.35%");
options.addItem("15 years at 5.5%");
options.addItem("30 years at 5.75%");
Tlabel = new JLabel("Term (years)");
con.add(Tlabel);
Ttextfield = new JTextField("");
con.add(Ttextfield);
Rlabel = new JLabel("Interest Rate");
con.add(Rlabel);
Rtextfield = new JTextField("");
con.add(Rtextfield);
Plabel = new JLabel("Monthly Payment Amount");
con.add(Plabel);
$label = new JLabel("");
con.add($label);
calculate = new JButton("Calculate");
con.add(calculate);
calculate.setBackground(Color.white);
reset = new JButton("Clear");
con.add(reset);
reset.setBackground(Color.white);
end = new JButton ("End");
con.add(end);
end.setBackground(Color.white);
//table header names
String[] columnNames = {"Payment #","Payment Amount", "Interest", "Principle Reduction",
"Remaining Balance"};
//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 ("See Graph");
con.add(graph);
graph.setBackground(Color.white);
//action listeners
Ltextfield.addActionListener(this);
options.addActionListener(this);
calculate.addActionListener(this);
reset.addActionListener(this);
end.addActionListener(this);
graph.addActionListener(this);
}
//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 == end)
{
exit();
}
if (source == mnuExit)
{
exitGraph();
}
if (source == graph)
{
mFrame = new JFrame("Mortgage Graph");
mFrame.getContentPane().add(new GraphPanel(yearlyPrinciple, yearlyInterest));
mFrame.setSize(800,600);
mFrame.setLocation(200,100);
//trying to create menu
// 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 ("End", true);
mnuBar.add(mnuExitbar);
mnuExitbar.add(mnuExit);
mFrame.setVisible(true);
//exit listener
mnuExit.addActionListener(this);
}
}
public JFrame mFrame = new JFrame();
void exitGraph()
{
mFrame.dispose();
mFrame = null;
}
void setRate()
{
int index = options.getSelectedIndex();
//term and interest error check
if (index > 0)
{
try
{
Ttextfield.setText(Integer.toString(trmArray[index-1]));
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term. Please try
again!",
"Message Dialog",
JOptionPane.PLAIN_MESSAGE);
Ttextfield.setText(null);
}
try
{
Rtextfield.setText(Double.toString(intrstArray[index-1]));
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate. Please
try again!",
"Message Dialog",
JOptionPane.PLAIN_MESSAGE);
Rtextfield.setText(null);
}
}
}
//calculation section
void startCalculations()
{
Thread thisThread = Thread.currentThread();
NumberFormat currency = NumberFormat.getCurrencyInstance();
double amt = 0;
double trm = 0;
double intrst = 0;
double moIn = 0;
double moTrm = 0;
double prin = 0;
double paymt = 0;
//amount error check
try
{
amt = Double.parseDouble(Ltextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Missing Amount or Use of Commas",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ltextfield.setText(null);
Ttextfield.setText(null);
Rtextfield.setText(null);
options.setSelectedIndex(0);
}
//term and interest error check
try
{
trm = Double.parseDouble(Ttextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term. Please try
again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ttextfield.setText(null);
}
try
{
intrst = Double.parseDouble(Rtextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate. Please try
again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Rtextfield.setText(null);
}
if (amt > 0)
{
amt = Double.parseDouble(Ltextfield.getText());
moIn = (intrst / 1200);//monthly interest rate
moTrm = trm * 12;//number of payments
paymt = (amt * moIn) / (1-Math.pow((1+moIn), -moTrm));//amount forumla
yearlyPrinciple = new float[(int)trm]; // initialize the arrays to store yearly
principle and interest
yearlyInterest = new float[(int)trm];
$label.setText("" + currency.format(paymt));
double newPrin = amt;
for (int i = 0; i < trm; i++)
{
yearlyInterest[i] = 0.0f;
yearlyPrinciple[i] = 0.0f;
for(int j = 1; j <=12; j++)
{
double newIn = moIn * newPrin;
double reduct = paymt - newIn;
yearlyInterest[i] += newIn;
yearlyPrinciple[i] += reduct;
newPrin = newPrin - reduct;
if (newPrin < 0)
newPrin = 0;
else
newPrin = newPrin;
model.addRow(new Object[] { Integer.toString((i*12) + j),
currency.format(paymt),
currency.format(newIn), currency.format(reduct), currency.format(newPrin) });
}
// model.addRow(new Object[] { Integer.toString(i), currency.format(0.0),
// currency.format(yearlyInterest[i]), currency.format(yearlyPrinciple[i]),
currency.format(0.0) });
}
}
//less than 0 error check
if (amt < 0)
{
JOptionPane.showMessageDialog(null, "Please Enter Positie Numbers Only.",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ltextfield.setText(null);
}
}
//resets all fields
void reset()
{
Ltextfield.setText(null);
Ttextfield.setText(null);
Rtextfield.setText(null);
options.setSelectedIndex(0);
$label.setText(null);
model.setRowCount(0);
}
//exit the program with thank you message
void exit()
{
JOptionPane.showMessageDialog(null, " Thank you for using \n Mortgage
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 Mort6().setVisible(true);
}
});
}
}
//creates class for container layout and placement
class Mort6Layout implements LayoutManager{
public Mort6Layout() {}
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);}
}
}
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);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
int w = getWidth();
int h = getHeight();
// scales
float xInc = (w - HPAD - VPAD) / (interestData.length - 1);//11f;
float yInc = (h - 2*VPAD) / 10f;
int[] dataVals = getDataVals(); //min and max values for y-axis
float yScale = dataVals[2] / 10f;
// ordinate (y - axis)
g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
// plot tic marks
float x1 = HPAD, y1 = VPAD, x2 = HPAD - 3, y2;
for(int j = 0; j < 10; j++)
{
g2.draw(new Line2D.Double(x1, y1, x2, y1));
y1 += yInc;
}
// labels
String text; LineMetrics lm;
float xs, ys, textWidth, height;
for(int j = 0; j <= 10; j++)
{
text = String.valueOf(dataVals[1] - (int)(j * yScale));
textWidth = (float)font.getStringBounds(text, frc).getWidth();
lm = font.getLineMetrics(text, frc);
height = lm.getAscent();
xs = HPAD - textWidth - 7;
ys = VPAD + (j * yInc) + height/2;
g2.drawString(text, xs, ys);
}
// abcissa (x - axis)
g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
// tic marks
x1 = HPAD; y1 = h - VPAD; y2 = y1 + 3;
for(int j = 0; j < interestData.length; j++)
{
g2.draw(new Line2D.Double(x1, y1, x1, y2));
x1 += xInc;
}
// labels
ys = h - VPAD;
for(int j = 0; j < interestData.length; j++)
{
text = String.valueOf(j + 1);
textWidth = (float)font.getStringBounds(text, frc).getWidth();
lm = font.getLineMetrics(text, frc);
height = lm.getHeight();
xs = HPAD + j * xInc - textWidth/2;
g2.drawString(text, xs, ys + height);
}
// plot data
float yy2 = 0, yy1 = 0, xx2 = 0, xx1;
x1 = HPAD;
xx1 = HPAD;
yScale = (float)(h - 2*VPAD) / dataVals[2];
for(int j = 0; j < interestData.length; j++)
{
g.setColor(Color.blue);
y1 = VPAD + (h - 2*VPAD) - (principleData[j] - dataVals[0]) * yScale;
if(j > 0)
g2.draw(new Line2D.Double(x1, y1, x2, y2));
x2 = x1;
y2 = y1;
x1 += xInc;
g.setColor(Color.red);
yy1 = VPAD + (h - 2*VPAD) - (interestData[j] - dataVals[0]) * yScale;
if(j > 0)
g2.draw(new Line2D.Double(xx1, yy1, xx2, yy2));
xx2 = xx1;
yy2 = yy1;
xx1 += xInc;
}
}
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 };
}
}
You might have the numbers in the wrong order: you're calling parseInt on a double
You need to call parseInt followed by parseDouble on the same line read that you split
There is a problem with the logic here.
read line reads a line at a time. your logic think that readline reads a column at a time.
if you don't know the length of your file, you need to use a dynamically growing data structure to store the mortgages, or continually regrow your array (inefficient)
read line reads a line at a time. your logic think that readline reads a column at a time.
if you don't know the length of your file, you need to use a dynamically growing data structure to store the mortgages, or continually regrow your array (inefficient)
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());
}
for ( int i = 0; i < line.length; i++ )
{
trmArray[ i ] = Integer.parseInt(line[i].t rim());
}
You can't call parseint on a decimal. You need to change trmArray to a double array and rewrite your program in order to use it as a double array. You'll need this line to be trmArray[i] = Double.parseDouble(line[i] .trim());
Good luck
{
trmArray[ i ] = Integer.parseInt(line[i].t
}
You can't call parseint on a decimal. You need to change trmArray to a double array and rewrite your program in order to use it as a double array. You'll need this line to be trmArray[i] = Double.parseDouble(line[i]
Good luck
ASKER
Ok I changed trmArray[i] = Double.parseDouble(line[i] .trim());
and I can call the first rate and term, but I cant call the other two.
How can I call the external file to read the rows?
Like this
7,5.35
15,5.50
30,5.75
and I can call the first rate and term, but I cant call the other two.
How can I call the external file to read the rows?
Like this
7,5.35
15,5.50
30,5.75
public void load()
{
Reader fis;
try
{
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String[] line = b.readLine( ).split(",");
trmArray = new double[line.length];
for ( int i = 0; i < line.length; i++ )
{
trmArray[ i ] = Double.parseDouble(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());
}
b.close();
fis.close();
}
catch ( Exception e1 )
{
e1.printStackTrace( );
}
}
All of your code is a bit off. I'm going to assume that you're only using those 3 lines with no plans to expand this. If you plan to expand this, then you need to look at the arraylist class and learn about objects that are capable of expanding their size easier than an array. The proper way to do it would be:
try
{
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String inputholder = "";
String termsandrates = "";
while ((inputholder = b.readLine()) != null)
{
termsandrates = termsandrates + inputholder;
}
String[] line = termsandrates.split(",");
trmArray = new double[3];
trmArray[0] = Double.parseDouble(line[0] .trim());
trmArray[1] = Double.parseDouble(line[2] .trim());
trmArray[2] = Double.parseDouble(line[4] .trim());
intrstArray = new double[3];
intrstArray[0] = Double.parseDouble(line[1] .trim());
intrstArray[1] = Double.parseDouble(line[3] .trim());
intrstArray[2] = Double.parseDouble(line[5] .trim());
b.close();
fis.close();
}
This isn't the greatest way of coding, but it should fix your problem.
try
{
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String inputholder = "";
String termsandrates = "";
while ((inputholder = b.readLine()) != null)
{
termsandrates = termsandrates + inputholder;
}
String[] line = termsandrates.split(",");
trmArray = new double[3];
trmArray[0] = Double.parseDouble(line[0]
trmArray[1] = Double.parseDouble(line[2]
trmArray[2] = Double.parseDouble(line[4]
intrstArray = new double[3];
intrstArray[0] = Double.parseDouble(line[1]
intrstArray[1] = Double.parseDouble(line[3]
intrstArray[2] = Double.parseDouble(line[5]
b.close();
fis.close();
}
This isn't the greatest way of coding, but it should fix your problem.
ASKER
should I format the file a different way, I still get errors
java.lang.ArrayIndexOutOfB oundsExcep tion: 4
at Mort6.load(Mort6.java:100)
at Mort6.<init>(Mort6.java:73 )
at Mort6$1.run(Mort6.java:429 )
at java.awt.event.InvocationE vent.dispa tch(Invoca tionEvent. java:209)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:597)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Even tDispatchT hre
ad.java:269)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(EventDi spatchThre ad.
java:184)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:174)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 69)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 61)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:122)
java.lang.ArrayIndexOutOfB
at Mort6.load(Mort6.java:100)
at Mort6.<init>(Mort6.java:73
at Mort6$1.run(Mort6.java:429
at java.awt.event.InvocationE
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThre
java:184)
at java.awt.EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
That was my fault, I left a step out. Try this instead:
try
{
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String inputholder = "";
String termsandrates = "";
while ((inputholder = b.readLine()) != null)
{
termsandrates = termsandrates + "," + inputholder;
}
String[] line = termsandrates.split(",");
trmArray = new double[3];
trmArray[0] = Double.parseDouble(line[0] .trim());
trmArray[1] = Double.parseDouble(line[2] .trim());
trmArray[2] = Double.parseDouble(line[4] .trim());
intrstArray = new double[3];
intrstArray[0] = Double.parseDouble(line[1] .trim());
intrstArray[1] = Double.parseDouble(line[3] .trim());
intrstArray[2] = Double.parseDouble(line[5] .trim());
b.close();
fis.close();
}
try
{
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String inputholder = "";
String termsandrates = "";
while ((inputholder = b.readLine()) != null)
{
termsandrates = termsandrates + "," + inputholder;
}
String[] line = termsandrates.split(",");
trmArray = new double[3];
trmArray[0] = Double.parseDouble(line[0]
trmArray[1] = Double.parseDouble(line[2]
trmArray[2] = Double.parseDouble(line[4]
intrstArray = new double[3];
intrstArray[0] = Double.parseDouble(line[1]
intrstArray[1] = Double.parseDouble(line[3]
intrstArray[2] = Double.parseDouble(line[5]
b.close();
fis.close();
}
ASKER
By the way, thank you for all the help
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String inputholder = "";
String termsandrates = "";
while ((inputholder = b.readLine()) != null)
{
termsandrates = termsandrates + "," + inputholder;
}
String[] line = termsandrates.split(",");
trmArray = new double[3];
trmArray[0] = Double.parseDouble(line[0] .trim());
trmArray[1] = Double.parseDouble(line[2] .trim());
trmArray[2] = Double.parseDouble(line[4] .trim());
intrstArray = new double[3];
intrstArray[0] = Double.parseDouble(line[1] .trim());
intrstArray[1] = Double.parseDouble(line[3] .trim());
intrstArray[2] = Double.parseDouble(line[5] .trim());
b.close();
fis.close();
}
I still get errors
java.lang.NumberFormatExce ption: empty String
at sun.misc.FloatingDecimal.r eadJavaFor matString( FloatingDe cimal.java :99
4)
at java.lang.Double.parseDoub le(Double. java:510)
at Mort6.load(Mort6.java:98)
at Mort6.<init>(Mort6.java:73 )
at Mort6$1.run(Mort6.java:428 )
at java.awt.event.InvocationE vent.dispa tch(Invoca tionEvent. java:209)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:597)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Even tDispatchT hre
ad.java:269)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(EventDi spatchThre ad.
java:184)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:174)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 69)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 61)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep tion
at Mort6.setRate(Mort6.java:2 83)
at Mort6.actionPerformed(Mort 6.java:217 )
at javax.swing.JComboBox.fire ActionEven t(JComboBo x.java:124 0)
at javax.swing.JComboBox.setS electedIte m(JComboBo x.java:567 )
at javax.swing.JComboBox.setS electedInd ex(JComboB ox.java:60 3)
at javax.swing.plaf.basic.Bas icComboPop up$Handler .mouseRele ased(Basic Com
boPopup.java:816)
at java.awt.AWTEventMulticast er.mouseRe leased(AWT EventMulti caster.jav a:2
73)
at java.awt.Component.process MouseEvent (Component .java:6216 )
at javax.swing.JComponent.pro cessMouseE vent(JComp onent.java :3265)
at javax.swing.plaf.basic.Bas icComboPop up$1.proce ssMouseEve nt(BasicCo mbo
Popup.java:480)
at java.awt.Component.process Event(Comp onent.java :5981)
at java.awt.Container.process Event(Cont ainer.java :2041)
at java.awt.Component.dispatc hEventImpl (Component .java:4583 )
at java.awt.Container.dispatc hEventImpl (Container .java:2099 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:4413)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:4 556
)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:42 20)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:4150)
at java.awt.Container.dispatc hEventImpl (Container .java:2085 )
at java.awt.Window.dispatchEv entImpl(Wi ndow.java: 2475)
at java.awt.Component.dispatc hEvent(Com ponent.jav a:4413)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:599)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Even tDispatchT hre
ad.java:269)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(EventDi spatchThre ad.
java:184)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:174)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 69)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 61)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:122)
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String inputholder = "";
String termsandrates = "";
while ((inputholder = b.readLine()) != null)
{
termsandrates = termsandrates + "," + inputholder;
}
String[] line = termsandrates.split(",");
trmArray = new double[3];
trmArray[0] = Double.parseDouble(line[0]
trmArray[1] = Double.parseDouble(line[2]
trmArray[2] = Double.parseDouble(line[4]
intrstArray = new double[3];
intrstArray[0] = Double.parseDouble(line[1]
intrstArray[1] = Double.parseDouble(line[3]
intrstArray[2] = Double.parseDouble(line[5]
b.close();
fis.close();
}
I still get errors
java.lang.NumberFormatExce
at sun.misc.FloatingDecimal.r
4)
at java.lang.Double.parseDoub
at Mort6.load(Mort6.java:98)
at Mort6.<init>(Mort6.java:73
at Mort6$1.run(Mort6.java:428
at java.awt.event.InvocationE
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThre
java:184)
at java.awt.EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep
at Mort6.setRate(Mort6.java:2
at Mort6.actionPerformed(Mort
at javax.swing.JComboBox.fire
at javax.swing.JComboBox.setS
at javax.swing.JComboBox.setS
at javax.swing.plaf.basic.Bas
boPopup.java:816)
at java.awt.AWTEventMulticast
73)
at java.awt.Component.process
at javax.swing.JComponent.pro
at javax.swing.plaf.basic.Bas
Popup.java:480)
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
)
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThre
java:184)
at java.awt.EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
YOU RULE!!!! That did it, thanks for all your help!!! You made someone smile today.