|
-
Apr 6th, 2001, 11:12 AM
#1
Thread Starter
Hyperactive Member
Anybody know why I my little application errors out when I click "Accept"?
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Money extends JFrame implements ActionListener
{
//All of my items that will be placed on my panels
JButton close;
JButton accept;
JTextField entryOne;
JTextField entryTwo;
JLabel lblOne;
JLabel lblTwo;
JLabel lblResults;
TotalMoney amountOne;
TotalMoney amountTwo;
public Money()
{
super("Morgan");
Container container = getContentPane();
container.setLayout(new BorderLayout());
//Panels
JPanel panelMain = new JPanel();
JPanel panelEntry = new JPanel();
JPanel panelResults = new JPanel();
panelEntry.setLayout(new GridLayout(5,2));
panelResults.setLayout(new GridLayout(2,1));
//Buttons
accept = new JButton("Accept");
close = new JButton("Close");
//Text Fields
entryOne = new JTextField(2);
entryTwo = new JTextField(2);
//Labels
lblOne = new JLabel("First Amount");
lblTwo = new JLabel("Second Amount");
lblResults = new JLabel();
lblOne.setHorizontalAlignment(SwingConstants.RIGHT);
lblTwo.setHorizontalAlignment(SwingConstants.RIGHT);
lblResults.setHorizontalAlignment(SwingConstants.CENTER);
//Add all items to panel
panelMain.add(accept);
panelMain.add(close);
panelEntry.add(lblOne);
panelEntry.add(entryOne);
panelEntry.add(lblTwo);
panelEntry.add(entryTwo);
panelResults.add(lblResults);
//Action Listener
close.addActionListener(this);
accept.addActionListener(this);
//Add Panels to Frame
container.add(panelMain,BorderLayout.SOUTH);
container.add(panelEntry,BorderLayout.NORTH);
container.add(panelResults,BorderLayout.CENTER);
}
//Main method
public static void main(String[] args)
{
Money frame = new Money();
frame.setSize(300,200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == close) //User has pressed the Close button
{
//Exit program gracefully
System.exit(0);
}
if(event.getSource() == accept) //Use has pressed the Accept button
{
/*
//Determine which is bigger
if (entryOne.getText().compareTo(entryTwo.getText()) > 0)
lblResults.setText(entryOne.getText() + " is bigger");
else
lblResults.setText(entryTwo.getText() + " is bigger");
*/
//Money Items
amountOne = new TotalMoney("1.2");
// amountTwo = new TotalMoney(entryTwo.getText());
// System.out.println(amountOne + " " + amountTwo);
}//End if
}//End actionPerformed
}
//TotalMoney with 2 constructors
class TotalMoney
{
long change;
//Default constructor
TotalMoney()
{
change = 0;
}
//Contructor that accepts an arg
TotalMoney(String c)
{
change = Long.parseLong(c,10);
}
/*
//Compare the amounts
long compare()
{
//I have no clue what to do here.... UGH!!
return change;
}
*/
}
-
Apr 6th, 2001, 12:02 PM
#2
Hyperactive Member
Rockie it is erroring because I think you are attempting to use parseLong method of Long, which will parse a string which is a valid long, which in your case it is not.
You pass it 1.2 this is not a long but a double or float. Longs are just larger ints, you cant have an int declared 1.2 can you? So the same thing applies here. It is trying to grab a long and it sees a float/double and that is why it is throwing the NumberFormatException.
-
Apr 6th, 2001, 12:07 PM
#3
Hyperactive Member
What I would would do is use a stringTokenizier to split the string up with . as seperator. Then do a getNextToken() which will return the dollar amount, which you can then times by a 100 to get the number of cents, do another getNextToken() which will return the right side of your string which is the cents, and then add that to dollar(which you have in cents now). This you could store in a long.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|