0
    public class CustomCalculator extends Frame implements ActionListener{

Panel jp1 = new Panel();
Panel jp2 = new Panel();
Panel jp3 = new Panel();
Panel jp4 = new Panel();
Panel jp5 = new Panel();
Panel center_merge = new Panel();


Label l2 = new Label("Quantity : ");
TextField l2a = new TextField(20);

Label l3 = new Label("Invoice Value : ");
TextField l3a = new TextField(20);

Label l4 = new Label("Exchange Rate : ");
TextField l4a = new TextField(20);

Label l5 = new Label("Costing(A) : ");
TextField l5a = new TextField();

Label l6 = new Label("(A + 1%)(B) : ");
Label l6a = new Label();

Label l7 = new Label("BCD (C) : ");
Label l7a = new Label("");

Label l8 = new Label("CVD (D) : ");
Label l8a = new Label("");

Label l9 = new Label("Custom Education Cess (E) : ");
Label l9a = new Label("");

Label l10 = new Label("Custom Sec & Higher Edu.Cess (F) : ");
Label l10a = new Label("");

Label l11 = new Label("Additional Duty Imports (G) : ");
Label l11a = new Label("");

Label l12 = new Label("Total (H) : ");
Label l12a = new Label("");

Label l13 = new Label("Costing+Total (I) : ");
Label l13a = new Label("");

Label l14 = new Label("(H/Quantity) (J) : ");
Label l14a = new Label("");

Label l15 = new Label("4% SAD (G/Quantity) (K) : ");
Label l15a = new Label("");

Label l16 = new Label("Net Costing (L) : ");
Label l16a = new Label("");

Label l17 = new Label("Transportation (M) : ");
TextField l17a = new TextField(5);

Label l18 = new Label("Godown Rate (N) : ");
TextField l18a = new TextField(5);

Label l19 = new Label("Brokerage (O) : ");
TextField l19a = new TextField(5);

Label l20 = new Label("Actual Costing (P) : ");
Label l20a = new Label("");

Label l21 = new Label("Small Gatepass (Q) : ");
Label l21a = new Label("");

Label l22 = new Label("Big Gatepass (R) : ");
Label l22a = new Label("");

Button l2b = new Button("reset");
Button l3b = new Button("reset");
Button l4b = new Button("reset");


Button master_reset = new Button("reset all");
Button calc = new Button("Calculate");



public CustomCalculator()
{
    super("Custom Calculator");
    this.setSize(800,700);

    jp1.setLayout(new FlowLayout());
    //jp1.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp1.add(l2);
    jp1.add(l2a);
    jp1.add(l2b);

    jp1.add(l3);
    jp1.add(l3a);
    jp1.add(l3b);

    jp1.add(l4);
    jp1.add(l4a);
    jp1.add(l4b);

    jp2.setLayout(new GridLayout(6,2));
    //jp2.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp2.add(l5);
    jp2.add(l5a);

    jp2.add(l6);
    jp2.add(l6a);

    jp2.add(l7);
    jp2.add(l7a);

    jp2.add(l8);
    jp2.add(l8a);

    jp2.add(l9);
    jp2.add(l9a);

    jp2.add(l10);
    jp2.add(l10a);

    jp3.setLayout(new GridLayout(6,2));
    //jp3.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp3.add(l11);
    jp3.add(l11a);

    jp3.add(l12);
    jp3.add(l12a);

    jp3.add(l13);
    jp3.add(l13a);

    jp3.add(l14);
    jp3.add(l14a);

    jp3.add(l15);
    jp3.add(l15a);

    jp3.add(l16);
    jp3.add(l16a);

    jp4.setLayout(new GridLayout(6,2));
    //jp4.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp4.add(l17);
    jp4.add(l17a);

    jp4.add(l18);
    jp4.add(l18a);

    jp4.add(l19);
    jp4.add(l19a);

    jp4.add(l20);
    jp4.add(l20a);

    jp4.add(l21);
    jp4.add(l21a);

    jp4.add(l22);
    jp4.add(l22a);

    center_merge.setLayout(new GridLayout(1,3));
    //center_merge.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    center_merge.add(jp2);
    center_merge.add(jp3);
    center_merge.add(jp4);

    jp5.setLayout(new FlowLayout());
    //jp5.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    jp5.add(calc);
    jp5.add(master_reset);

    this.setLayout(new BorderLayout());

    this.add(jp1,BorderLayout.NORTH);
    this.add(center_merge,BorderLayout.CENTER);
    this.add(jp5,BorderLayout.SOUTH);

    this.addWindowListener(new WindowAdapter(){   
        public void windowClosing(WindowEvent we)
        {
            System.exit(0);
        }
    });

    l2b.addActionListener(this);
    l3b.addActionListener(this);
    l4b.addActionListener(this);
    calc.addActionListener(this);
    master_reset.addActionListener(this);

    this.setVisible(true);

}


public static void main(String[] args) {
    new CustomCalculator();

}


@Override
public void actionPerformed(ActionEvent ae) {
    double quantity = 0;
    double invoice_value = 0;
    double exchange_rate = 0;
    double A=0;
    double B=0;
    double C=0;
    double D=0;
    double E=0;
    double F=0;
    double G=0;
    double H=0;
    double I=0;
    double J=0;
    double K=0;
    double L=0;
    double M = 0;
    double N = 0;
    double O=0;
    double P=0;
    double Q=0;
    double R=0;

try
{
    quantity = Double.parseDouble(l2a.getText());
    invoice_value = Double.parseDouble(l3a.getText());
    exchange_rate = Double.parseDouble(l4a.getText());
    M = Double.parseDouble(l17a.getText());
    N = Double.parseDouble(l18a.getText());
    O = Double.parseDouble(l19a.getText());

    A = invoice_value*exchange_rate;
    B = A+(0.01*A);
    C = 0.075*B;
    D = 0.12*(B+C);
    E = 0.02*(C+D);
    F = 0.01*(C+D);
    G = 0.04*(B+C+D+E+F);
    H = C+D+E+F+G;
    I = A+H;
    J = H/quantity;
    K = G/quantity;
    L = J-K;
    P = L+M+N+O;
    Q = (0.12*B)/quantity;
    R = Q+K;



    if(ae.getActionCommand().equals("calc"))
      {

        l5a.setText(String.valueOf(A));
        l6a.setText(String.valueOf(B));
        l7a.setText(String.valueOf(C));
        l8a.setText(String.valueOf(D));
        l9a.setText(String.valueOf(E));
        l10a.setText(String.valueOf(F)); 
        l11a.setText(String.valueOf(G));
        l12a.setText(String.valueOf(H));
        l13a.setText(String.valueOf(I));
        l14a.setText(String.valueOf(J));
        l15a.setText(String.valueOf(K));
        l16a.setText(String.valueOf(L));
        l20a.setText(String.valueOf(P));
        l21a.setText(String.valueOf(Q));
        l22a.setText(String.valueOf(R));        

      }
    else if(ae.getActionCommand().equals("master_reset"))
      {
            l5a.setText("");
            l2a.setText("");
            l3a.setText("");
            l4a.setText("");
    }
    }
    catch (Exception ex)
    {
        l5a.setText(ex.toString());
        // l3a.setText(ex.toString());
    }

    }

}

After I click the Calculate button (button calc) the calculated values do not appear in the respective labels and an exception is shown saying java.lang.NumberFormatException: Empty string. I am not able to figure out the solution. please help.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • The stacktrace should point you at least to the correct line. I would guess it somewhere near all of the Double.parseDouble(..). – SWoeste Mar 10 '14 at 15:38
  • Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Mar 11 '14 at 11:04

2 Answers2

2

the line exchange_rate = Double.parseDouble(l4a.getText()); gives you this exception, because there is no value in l4a and you are trying to parse it into a double value, try printing the exception in the catch clause.

rsudha
  • 289
  • 1
  • 4
  • 12
  • When I print the NumberFormatException, it gets printed in all the labels where actually the calculated values are to be printed. Unable to figure out the problem still. it seems that the values are not being accepted or something. please excuse me for my naivety. I am new to application building. – Nandini Mar 10 '14 at 16:00
  • it must be due to one of the value you are trying to cast to double is not a valid number (it is probably empty), I've tried inputting all the boxes except the l4a field because i couldnt see an input field for that. try doing a system.out.println of all the text fields you are trying to cast to double system.out.println("l2a.getText" + l2a.getText()); quantity = Double.parseDouble(l2a.getText()); and see if it has got a value, do the same on all other text fields conversion. – rsudha Mar 10 '14 at 16:07
0

It's probably a relatively safe bet to say that it is happening here at the beginning of your function, though you should provide the actual exception and line number for us.

quantity = Double.parseDouble(l2a.getText());
invoice_value = Double.parseDouble(l3a.getText());
exchange_rate = Double.parseDouble(l4a.getText());
M = Double.parseDouble(l17a.getText());
N = Double.parseDouble(l18a.getText());
O = Double.parseDouble(l19a.getText());

Make sure that each of these fields actually has numeric text in it. I would recommend putting a logging line or an alert line to state their values before this is executed. Better yet, you can catch the first line in a debugger and look at the the values of all of the items you're calling getText() on. I bet one has an empty string.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255