Reputation: 93
My issue is that my radio buttons causes a huge error in the java terminal when I select it. It is in the beginning buttons (button1 to button 4) button1 starts up the gas station (so you can choose what type of gas you want), but instead of opening up radio buttons to choose from, it gives me a bunch of errors that relates to
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
The radiobuttons worked before, but after I made the checkboxes method it stopped working, but the checkbox selection works (not clearing, however). Found the error, it belonged to this
litres=Double.parseDouble(gasL.getText());
So I temporarily removed that part, but not sure how to get it to work because I am trying to get the litres from the text box the user can input. The gas bar still doesn't open up
Clearing works for check boxes, but I am not sure for radio buttons, which gives me the errors
Here is my code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* A GUI for a Gas Bar, which allows you to choose the gas type you want, and optional car checks for things like
* windshield and gas check. The price is outputted afterwards.
*
* @author Kevin Hy
* @date 08/06/13
*/
public class New_Gas_Bar extends JFrame
{
public JPanel panel1,panel2,panel3,panel4,panel5;
public JLabel main1, main2, main3,main4,main5,main6;
public JLabel gasBar,total;
public JButton button1,button2,button3,button4,clearGas,clearCar;
public JRadioButton bronzeG,silverG,goldG,selfS,fullS,fullService,selfService;
public JCheckBox oilC,windWash,pressureClock;
public JTextField gasL;
static double fullCost,selfCost,discount,gasCost,litres,cleaningC;
static boolean boughtGas=false;
public New_Gas_Bar()
{
super (" New Gas Bar");
setSize(640,640);
Container container = getContentPane();
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();
panel5=new JPanel();
panel1.setBackground(new Color(107,202,226));
panel1.setLayout(new GridLayout(6,1));
main1 = new JLabel (" Gas Bar Project ");
main2 = new JLabel (" ICS4U0 - September 2013");
main3 = new JLabel (" ");
main4 = new JLabel ("Enter Litres of Gas");
main5 = new JLabel ("Select Full or Self Serve");
main6 = new JLabel ("Select Gas Quality");
button1 = new JButton ("Gas Station");
button2 = new JButton ("Car Wash");
button3 = new JButton ("Total");
button4 = new JButton ("Exit");
clearCar = new JButton ("Clear");
clearGas = new JButton ("Clear");
panel2.setBackground(new Color(144,160,170));
panel2.setLayout(new FlowLayout ());
panel2.add (button1);
panel2.add (button2);
panel2.add (button3);
panel2.add (button4);
panel1.add (main1);
panel1.add (main2);
panel1.add (main3);
//container.setLayout(new BorderLayout());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.CENTER);
container.add(panel3,BorderLayout.EAST);
container.add(panel4,BorderLayout.EAST);
panel3.setBackground(new Color(107,202,226));
panel4.setBackground(new Color(107,202,226));
ButtonHandler handler = new ButtonHandler ();
button1.addActionListener (handler);
button2.addActionListener (handler);
button3.addActionListener (handler);
button4.addActionListener (handler);
clearCar.addActionListener (handler);
clearGas.addActionListener (handler);
ButtonGroup serveStyle = new ButtonGroup();
fullS = new JRadioButton ("Full Serve");
selfS = new JRadioButton ("Self Serve");
serveStyle.add (fullS);
serveStyle.add (selfS);
ButtonGroup serveGas = new ButtonGroup();
bronzeG = new JRadioButton("Bronze Service");
silverG = new JRadioButton("Silver Service");
goldG = new JRadioButton("Gold Service");
serveGas.add (bronzeG);
serveGas.add (silverG);
serveGas.add (goldG);
gasL = new JTextField(5);
oilC = new JCheckBox("Oil Change");
windWash = new JCheckBox("Windshield Wash");
pressureClock = new JCheckBox("Air Pressure Check");
RadioButtonHandler radioHand = new RadioButtonHandler ();
bronzeG.addItemListener (radioHand);
silverG.addItemListener (radioHand);
goldG.addItemListener (radioHand);
fullS.addItemListener (radioHand);
selfS.addItemListener (radioHand);
CheckBoxHandler checkHand = new CheckBoxHandler();
oilC.addItemListener (checkHand);
windWash.addItemListener (checkHand);
pressureClock.addItemListener (checkHand);
setVisible(true);
}
public static void main (String [] args)
{
New_Gas_Bar application = new New_Gas_Bar();
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == button1)
{
panel1.setVisible(true);
panel2.setVisible(true);
panel3.setVisible(true);
panel4.setVisible(false);
panel3.setLayout(new GridLayout(10,1));
panel3.add(clearGas);
panel3.add(main5);
panel3.add(fullS);
panel3.add(selfS);
panel3.add(main6);
panel3.add(bronzeG);
panel3.add(silverG);
panel3.add(goldG);
panel3.add(main4);
panel3.add(gasL);
litres=Double.parseDouble(gasL.getText());
//JOptionPane.showMessageDialog (null, litres);
}
else if (event.getSource () == button2)
{
panel1.setVisible(true);
panel2.setVisible(true);
panel3.setVisible(false);
panel4.setVisible(true);
panel4.setLayout(new FlowLayout ());
panel4.add(clearCar);
panel4.add(oilC);
panel4.add(windWash);
panel4.add(pressureClock);
}
else if (event.getSource () == button3)
{
JOptionPane.showMessageDialog (null, "This is your total:");
JOptionPane.showMessageDialog (null, litres);
}
else if (event.getSource () == button4)
{
System.exit(0);
}
if (event.getSource () == clearGas)
{
JOptionPane.showMessageDialog (null, "Clearing All Selected Options");
bronzeG.setSelected(false);
silverG.setSelected(false);
goldG.setSelected(false);
fullS.setSelected(false);
selfS.setSelected(false);
gasCost=0;
boughtGas=false;
}
if (event.getSource () == clearCar)
{
JOptionPane.showMessageDialog (null, "Clearing All Selected Options");
pressureClock.setSelected(false);
windWash.setSelected(false);
oilC.setSelected(false);
cleaningC=0;
}
}
}
public class RadioButtonHandler implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if (event.getSource () == fullS)
{
JOptionPane.showMessageDialog (null, "Full Serve Selected");
fullCost =1.25;
boughtGas=true;
}
else if (event.getSource () == selfS)
{
JOptionPane.showMessageDialog (null, "Self Serve Selected");
selfCost =0.60;
boughtGas=true;
}
if (event.getSource () == bronzeG)
{
JOptionPane.showMessageDialog (null, "Bronze Gas Option Selected");
gasCost=0.50;
boughtGas=true;
}
if (event.getSource () == silverG)
{
JOptionPane.showMessageDialog (null, "Silver Gas Option Selected");
gasCost=0.75;
boughtGas=true;
}
if (event.getSource () == goldG)
{
JOptionPane.showMessageDialog (null, "Gold Gas Option Selected");
gasCost=0.95;
boughtGas=true;
}
}
}
public class CheckBoxHandler implements ItemListener
{
public void itemStateChanged (ItemEvent event)
{
if (event.getSource () == oilC)
{
JOptionPane.showMessageDialog (null, "Oil Cleaning Selected");
if (boughtGas==true)
{
cleaningC+=10.00;
}
else
{
cleaningC+=12.50;
}
}
if (event.getSource () == windWash)
{
JOptionPane.showMessageDialog (null, "Windshield Washing Selected");
if (boughtGas==true)
{
cleaningC+=10.00;
}
else
{
cleaningC+=12.50;
}
}
if (event.getSource () == pressureClock)
{
JOptionPane.showMessageDialog (null, "Air Pressure Check Selected");
if (boughtGas==true)
{
cleaningC+=10.00;
}
else
{
cleaningC+=12.50;
}
}
}
}
}
Upvotes: 1
Views: 1338
Reputation: 1021
Take a look at the error you are getting. It says NumberFormatException. I believe the problem lies in your Double.parseDouble(gasL.getText()). What happens when gasL is left blank? How will the program parse a null string into a double? Try setting the default value of gasL to 0 or look into try/catch to handle exceptions.
gasL = new JTextField("0",5);
This removes the error message but I cannot figure out why your Gas Station panel isn't opening.
Upvotes: 0
Reputation: 474
I think that it is best not to use methods such as: Double.parseDouble() directly.
Of course, you wouldn't want to develop your own methods to parse doubles, etc.
What I like to do is to map the empty string and even nulls into zero.
public static double myParseDouble(String s) {
if (s == null || s.equals("")) {
return 0.0;
} else {
return Double.parseDouble(s);
}
}
You have to do the same sort of thing when parsing ints, floats, longs, and BigIntegers.
I suggest that you take my method, in its own project, and "play with it", running various strings through it and see what it does.
Upvotes: 0