hey, am pretty new to java - i understand the basics as i have previously spent 2 years using Vb, am now having to use java and this problem has really thrown me...
im creating a java program to take user input (scenario is a bike hire store).
I am trying to collect user information in yes/no dialogs and plain user input dialogs, however I am receiving the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unk nown Source)
when trying to run the program. I was previously getting an error that variables weren't initializing (in the 'errors' pane in eclipse) and i am now receiving the above error in the 'console' pane. My program code and the full error message is below:
package bike_Hire;
import javax.swing.JOptionPane;
public class Customer_Details {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name, address, dob, contactTel, numOfBikes, output1, output2,
mBMale = "0", mBMaleSize = null, mBMaleHelmets = "0", mBFemale = "0", mBFemaleSize = "", mBFemaleHelmets = "0",
rBMale = "0", rBMaleSize, rBMaleHelmetPanners = "0", rBFemale = "0", rBFemaleSize, rBFemaleHelmetPanners = "0",
maleHelmetPanners = "0",
cBike, cBikeAge;
int mBMaleInt, mBFemaleInt, rBMaleInt, rBFemaleInt, cBikeInt, bikeTotalInt, numOfBikesInt,
mBTotalFrames, mBFramesCost, mBTotalHelmets, mBMaleHelmetsInt, mBFemaleHelmetsInt, mBHelmetCost,
mBTotalCost, rBFramesCost, rBTotalFrames, rBTotalHelmets, rBMaleHelmetInt, userResponse1,
userResponse2, userResponse3, userResponse4,rBFemaleHelmetInt, rBHelmetCost, rBTotalCost,
cBikeTotal, cBikeTotalCost, TotalCostInt;
JOptionPane.showMessageDialog(null, "INSTRUCTIONS \nIf you do not require any of the following options, please enter 0" +
"\n\nInstructions Marked * Are Mandatory");
name = JOptionPane.showInputDialog("Please Enter Your Full Name*");
address = JOptionPane.showInputDialog("Please Enter Your Current Address*");
dob = JOptionPane.showInputDialog("Please Enter Your Date of Birth (DD/MM/YYYY)*");
contactTel = JOptionPane.showInputDialog("Please Enter a Contact Telephone Number*");
//mBMale = "0"; mBMaleSize = ""; mBMaleHelmets = "0";
//mBFemale = "0"; mBFemaleSize = ""; mBFemaleHelmets = "0";
userResponse1 = JOptionPane.showConfirmDialog(null, "Do You Require Any Male Mountain Bikes? (£20 each)", "Male Mountain Bikes", JOptionPane.YES_NO_OPTION);
if(userResponse1 == JOptionPane.YES_OPTION){
mBMale = JOptionPane.showInputDialog("Please Enter the number of Male Mountain Bikes Required (Leaving this field blank will automatically choose 1):");
if(mBMale == null) mBMale = "1";
System.out.println(mBMale);
mBMaleSize = JOptionPane.showInputDialog("Please Enter the Frame Size Required (Small/Medium/Large) (Leaving this field blank will automatically select Medium):");
if(mBMaleSize == null) mBMaleSize = "Medium";
mBMaleHelmets = JOptionPane.showInputDialog("How Many Helmets Do You Require? (Leaving this field blank will automatically choose 1)");
if(mBMaleHelmets == null) mBMaleHelmetsInt = 1;
} else if(userResponse1 == JOptionPane.NO_OPTION){
mBMale = "0"; mBMaleSize = ""; mBMaleHelmets = "0";
}
userResponse3 = JOptionPane.showConfirmDialog(null, "Do You Require Any Female Mountain Bikes? (£20 each)", "Female Mountain Bikes", JOptionPane.YES_NO_OPTION);
if(userResponse3 == JOptionPane.YES_OPTION){
mBFemale = JOptionPane.showInputDialog("Please Enter the number of Female Mountain Bikes Required (Leaving this field blank will automatically choose 1):");
if(mBFemale == null) mBFemale = "1";
mBFemaleSize = JOptionPane.showInputDialog("Please Enter the Frame Size Required (Small/Medium/Large) (Leaving this field blank will automatically select Medium):");
if (mBFemaleSize == null) mBFemaleSize = "Medium";
userResponse4 = JOptionPane.showConfirmDialog(null, "Are Helmets Required?"
+ "Helmets are optional - £5 each", "Helmets?", JOptionPane.YES_NO_OPTION);
if(userResponse4 == JOptionPane.YES_OPTION){
mBFemaleHelmets = JOptionPane.showInputDialog("How Many Helmets Do You Require? (Leaving this field blank will automatically choose 1)");
if(mBFemaleHelmets == null) mBFemaleHelmets = "1";
} else if(userResponse4 == JOptionPane.NO_OPTION){
mBFemaleHelmets = "0";
}
}else if(userResponse3 == JOptionPane.NO_OPTION){
mBFemale = "0"; mBFemaleSize = ""; mBFemaleHelmets = "0";
}
rBMale = JOptionPane.showInputDialog("Please Enter the number of Male Racing Bikes Required (£25 each):");
if(rBMale == null) rBMale = "0";
rBMaleSize = JOptionPane.showInputDialog("Please Enter the Frame Size Required (Small/Medium/Large):");
rBMaleHelmetPanners = JOptionPane.showInputDialog("How Many Helmets & Panners Sets are Require? (optional - £10 each)");
if(rBMaleHelmetPanners == null) rBMaleHelmetPanners = "0";
rBFemale = JOptionPane.showInputDialog("Please Enter the number of Female Racing Bikes Required (£25 each)");
if(rBFemale == null) rBFemale = "0";
rBFemaleSize = JOptionPane.showInputDialog("Please Enter the Frame Size Required (Small/Medium/Large)");
rBFemaleHelmetPanners = JOptionPane.showInputDialog("How Many Helmet & Panner Sets are Required?");
if(rBFemaleHelmetPanners == null) rBFemaleHelmetPanners = "0";
cBike = JOptionPane.showInputDialog("Please Enter the Number of Children's Bikes Required (£15 each - Helmet Included)");
if(cBike == null) cBike = "0";
cBikeAge = JOptionPane.showInputDialog("Please Enter the Age of the Child (ages 4 - 14 available):");
if(cBikeAge == null) cBikeAge = "0";
mBMaleInt = Integer.parseInt(mBMale); Error refers to HERE!
mBFemaleInt = Integer.parseInt(mBFemale);
rBMaleInt = Integer.parseInt(rBMale);
rBFemaleInt = Integer.parseInt(rBFemale);
mBMaleHelmetsInt = Integer.parseInt(mBMaleHelmets);
mBFemaleHelmetsInt = Integer.parseInt(mBFemaleHelmets);
rBMaleHelmetInt = Integer.parseInt(rBMaleHelmetPanners);
rBFemaleHelmetInt = Integer.parseInt(rBFemaleHelmetPanners);
cBikeTotal = Integer.parseInt(cBike);
mBTotalFrames = mBMaleInt + mBFemaleInt;
rBTotalFrames = rBMaleInt + rBFemaleInt;
mBFramesCost = mBTotalFrames * 20;
mBTotalHelmets = mBMaleHelmetsInt + mBFemaleHelmetsInt;
mBHelmetCost = mBTotalHelmets * 5;
mBTotalCost = mBFramesCost + mBHelmetCost;
rBFramesCost = rBTotalFrames * 25;
rBTotalHelmets = rBMaleHelmetInt + rBFemaleHelmetInt;
rBHelmetCost = rBTotalHelmets * 10;
rBTotalCost = rBFramesCost + rBHelmetCost;
cBikeTotalCost = cBikeTotal * 15;
bikeTotalInt = mBTotalFrames + rBTotalFrames + cBikeTotal;
TotalCostInt = mBTotalCost + rBTotalCost + cBikeTotalCost;
output1 = "Name: " + name +
" \nAddress " + address +
" \nD.O.B: " + dob +
" \nContact Tel: " + contactTel;
output2 = "Order Name: " + name +
"\nMale Mountain Bikes: " + mBMale +
"\nSize: " + mBMaleSize +
"\nFemale Mountain Bikes: " + mBFemale +
"\nSize: " + mBFemaleSize +
"\nMountain Bike Helmets: " + //mBTotalHelmets +
"\nCost of Mountain Bikes: £" + //mBTotalCost +
"\n\nMale Racing Bikes: " + rBMale +
"\nSize: " + rBMaleSize +
"\nFemale Racing Bikes: " + rBFemale +
"\nSize: " + rBFemaleSize +
"\nHelmet & Panners Sets: " + rBTotalHelmets +
"\nRacing Bikes Total Cost: £" + rBTotalCost +
"\n\nChildren's Bikes: " + cBikeTotal +
"\nHelmets Included" +
"\nChildren's Bikes Total Cost: £" + cBikeTotalCost +
"\n\nTOTAL COST: £" + TotalCostInt;
JOptionPane.showMessageDialog(null, output1, "Customer Details", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, output2, "Bike Hire Details", JOptionPane.INFORMATION_MESSAGE);
}
}
and the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at bike_Hire.Customer_Details.main(Customer_Details.j ava:87)
the problem seems to be when a null value is entered - although i have 'if' statements to correct nulls, it doesn't seem to be working...
if anyone can help it would be greatly appreciated, thanks!