-
Debug Please
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class Charge extends Applet implements ActionListener
{
Label lblPackage_ID = new Label("Package ID# ");
Label lblPackage_Pounds = new Label("Package Pounds");
Label lblPackage_Ounces = new Label("Package Ounces");
Label lblPackage_Pounds_Total = new Label(" ");
Label lblPackage_Ounces_Total = new Label(" ");
Label lblError = new Label("Error");
TextField txtPackage_ID = new TextField("154496P");
TextField txtPounds = new TextField("0");
TextField txtOunces = new TextField("5");
Button btnCalculate = new Button("Calculate");
//Declair Var's
final float fltSCharge = 0.12f;
final float fltCharge = 1.92f;
float fltPounds_Calculation;
float fltOunces_Calculation;
String strPounds;
String strOunces;
public void init()
{
setBackground(Color.darkGray);
add(lblPackage_ID);
add(txtPackage_ID);
add(lblPackage_Pounds);
add(txtPounds);
add(lblPackage_Ounces);
add(txtOunces);
add(btnCalculate);
lblError.setForeground(Color.red);
add(lblError);
add(lblPackage_Pounds_Total);
add(lblPackage_Ounces_Total);
}
public void actionPerformed(ActionEvent p1)
{
float fltPounds = Float.valueOf(txtPounds.getText()).floatValue();
float fltOunces = Float.valueOf(txtOunces.getText()).floatValue();
NumberFormat fmtDecimal = NumberFormat.getInstance();
fmtDecimal.setMaximumFractionDigits(2);
fmtDecimal.setMinimumFractionDigits(2);
NumberFormat fmtCurrency = NumberFormat.getCurrencyInstance();
if (fltPounds > 0)
{
fltPounds_Calculation = fltPounds * fltCharge;
}
if (fltOunces > 0)
{
fltOunces_Calculation = fltOunces * fltSCharge;
}
strPounds = String.valueOf(fltPounds_Calculation);
strOunces = String.valueOf(fltOunces_Calculation);
lblPackage_Pounds_Total.setText(strPounds);
lblPackage_Ounces_Total.setText(strOunces);
}
}
Its not displaying the Result in the Labels?
-
1- Use [c0de] [/c0de]
2-Write the error please.
-
its not outputing the results in the label
-
Thats because you didn't add the action listener to the button
Add this to your init
btnCalculate.addActionListener(this);
-
OMG DAHHHH thanks :-) little brain fart THANKS
-
ok now im haveing proplems with the calculations....
it works fine on the ounces up till i use 3??
it should be 36 not 35 and it it off bye 1 from then one ?????
Code:
/*
Programmer : Chad Eckles
Date : 9/14/02
Class : Charge
Folder : A:\ShippingCharge\ShippingCharge.jpx
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class Charge extends Applet implements ActionListener
{
Label lblPackage_ID = new Label("Package ID# ");
Label lblPackage_Pounds = new Label("Package Pounds");
Label lblPackage_Ounces = new Label("Package Ounces");
Label lblPackage_Pounds_Total = new Label(" ");
Label lblPackage_Ounces_Total = new Label(" ");
Label lblError = new Label("Error");
TextField txtPackage_ID = new TextField("154496P");
TextField txtPounds = new TextField("0");
TextField txtOunces = new TextField("5");
Button btnCalculate = new Button("Calculate");
//Declair Var's
final float fltSCharge = .12f;
final float fltCharge = 1.92f;
float fltPounds_Calculation;
float fltOunces_Calculation;
String strPounds;
String strOunces;
public void init()
{
setBackground(Color.darkGray);
add(lblPackage_ID);
add(txtPackage_ID);
add(lblPackage_Pounds);
add(txtPounds);
add(lblPackage_Ounces);
add(txtOunces);
btnCalculate.addActionListener(this);
add(btnCalculate);
lblError.setForeground(Color.red);
add(lblError);
add(lblPackage_Pounds_Total);
add(lblPackage_Ounces_Total);
}
public void actionPerformed(ActionEvent p1)
{
float fltPounds = Float.valueOf(txtPounds.getText()).floatValue();
float fltOunces = Float.valueOf(txtOunces.getText()).floatValue();
NumberFormat fmtDecimal = NumberFormat.getInstance();
fmtDecimal.setMaximumFractionDigits(2);
fmtDecimal.setMinimumFractionDigits(2);
NumberFormat fmtCurrency = NumberFormat.getCurrencyInstance();
if (fltPounds > 0)
{
fltPounds_Calculation = fltPounds * fltCharge;
}
if (fltOunces > 0)
{
fltOunces_Calculation = fltOunces * fltSCharge;
}
strPounds = String.valueOf(fltPounds_Calculation);
strOunces = String.valueOf(fltOunces_Calculation);
lblPackage_Pounds_Total.setText(strPounds);
lblPackage_Ounces_Total.setText(strOunces);
}
}
-
-
Not quite sure why you're using floats, but the problem comes there. Just use doubles. Can't really see that you'll go over that limit. Seems you're just wasting memory with floats.
-
Uh, floats use less memory than doubles...
-
They do. Well, guess you do learn something new everyday. How much memory does a float take?
-
4 bytes. double uses 8 bytes. That's what double means: double precision, thus double memory usage.