Results 1 to 2 of 2
  1. #1
    sumkindafreek is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default problems with java code! (very new - need help asap!)

    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!
    Last edited by sumkindafreek; 01-07-2009 at 05:56 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your error is I think assuming that the answer String will be null if the JOptionPane is left blank. The Exception however is telling you that this is not happening and in fact tells you exactly what is happening. This statement:
    Java Code:
    For input string: ""
    is telling you that a blank String is being used here, not a null variable.

    Other suggestions:
    1) Break this huge beast of a single method down into multiple smaller methods. As you're finding out, it's almost impossible to debug or enhance a program with huge monstrous methods such as this one.
    2) Avoid storing data as Strings when other data types just make more sense. For instance, you should use int data types when int data is being used, not String. convert the numeric String to an int and store the int. It just makes more sense and will make the code more readable and debuggable in the future.
    3) When posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:

    Java Code:
    [code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    [/code]
    Best of luck and HTH.

Similar Threads

  1. Java Problems
    By xonkie in forum New To Java
    Replies: 6
    Last Post: 12-03-2008, 08:14 PM
  2. [SOLVED] Threads Help, I think? Please Help ASAP!
    By tornado in forum New To Java
    Replies: 1
    Last Post: 11-30-2008, 05:33 AM
  3. Need Java Help ASAP
    By L-dog in forum New To Java
    Replies: 22
    Last Post: 10-03-2008, 12:54 AM
  4. Problems with this Java Program...
    By Bangtajra123 in forum Java Applets
    Replies: 6
    Last Post: 09-30-2008, 11:04 PM
  5. array problems need your help ASAP!
    By notherand in forum New To Java
    Replies: 1
    Last Post: 06-29-2008, 09:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •