This simple program should be able to get the Markup percentage and the wholesale Cost and calculate the retail price i put an action listener to the CALCULATE button but when i press the calculate button this error appears:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:
For input string: "Enter the markup precentage"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at mm$CalcListerner.actionPerformed(mm.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
Can someone help me with this issue?
Can someone please explain these error messages because I don’t really get any of that?
My code is:
import javax.swing.*;
import java.awt.event.*;
public class mm extends JFrame {
private JTextField WholesaleCost;
private JTextField markupPresentage;
private JLabel WCost;
private JLabel MPrecentage;
private JButton button;
private JPanel pannel;
private final int Width = 250;
private final int height = 320;
public mm() {
setTitle("Retail Price Calculator");
setSize(Width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
buildPanel();
add(pannel);
}
private void buildPanel() {
WholesaleCost = new JTextField(10);
markupPresentage = new JTextField(10);
WCost = new JLabel("enter the Whole Sale cost");
MPrecentage = new JLabel("Enter the markup precentage");
button = new JButton("Calculate");
button.addActionListener(new CalcListerner());
pannel = new JPanel();
pannel.add(WholesaleCost);
pannel.add(markupPresentage);
pannel.add(WCost);
pannel.add(MPrecentage);
pannel.add(button);
}
private class CalcListerner implements ActionListener {
public void actionPerformed(ActionEvent e) {
String WSaleinput;
String MPres;
WSaleinput = WholesaleCost.getText();
MPres = MPrecentage.getText();
double Value = Double.parseDouble(WSaleinput) * (Double.parseDouble(MPres) / 100);
JOptionPane.showMessageDialog(null, "Retail Price is " + Value);
}
}
public static void main(String[] args) {
mm x = new mm();
}
}
You’re trying to reference the wrong component.
This
Should actually be