Results 1 to 5 of 5
  1. #1
    sweetpea123 is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default if else statements

    I'm doing a car rental service application. The issue is that my if else statements aren't working correctly. One is a radibutton called payment with 4 credit cards, the user has to select one and only one. The other is some checkboxes the user can select none or all three. Here is my code.
    if(dr.isSelected()){
    Payment= "Discover";
    else if(md.isSelected())
    Payment = "MasterCard";
    else if(ax.isSelected())
    Payment = "American Express";
    else if(va.isSelected())
    Payment = "visa";


    }
    if(GPS.isSelected()){
    Optional = "GPS";
    else if(Bluetooth.isSelected())
    Optional = "Bluetooth";
    else if(Insurance.isSelected())
    Optional = "Insurance";


    }




    And this is the error that comes up.

    ---------- Capture Output ----------
    > "C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" RentalCar.java
    RentalCar.java:404: 'else' without 'if'
    else if(md.isSelected())
    ^
    RentalCar.java:415: 'else' without 'if'
    else if(Bluetooth.isSelected())
    ^
    2 errors

    > Terminated with exit code 1.


    Again any help you can give me would be appreciated.

  2. #2
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    If you open a bracket after the if statement, I believe it would read

    if(dr.isSelected()) {
    Payment= "Discover";
    else if(md.isSelected())
    Payment = "MasterCard";
    else if(ax.isSelected())
    Payment = "American Express";
    else if(va.isSelected())
    Payment = "visa";

    }

    All of the bold part in the case of if is true, thus your else if (in red) has no if before it, thus giving the error.

    Try closing each statement, or not opening a bracket after the if statement at all, like this:

    Java Code:
    if(dr.isSelected())
    Payment= "Discover";
    else if(md.isSelected()) 
    Payment = "MasterCard";
    else if(ax.isSelected()) 
    Payment = "American Express";
    else if(va.isSelected()) 
    Payment = "visa";
    	
    if(GPS.isSelected())
    Optional = "GPS";
    else if(Bluetooth.isSelected())
    Optional = "Bluetooth";
    else if(Insurance.isSelected())
    Optional = "Insurance";
    Last edited by Cemi; 04-12-2010 at 05:15 PM.
    Carpe Diem
    Each day's a gift and not a given right

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    It's one of those things that's generally recommended, and that's to use brackets for if/else...otherwise you will eventually be bitten by a bug.
    :)

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Tolls View Post
    It's one of those things that's generally recommended, and that's to use brackets for if/else...otherwise you will eventually be bitten by a bug.
    :)

    it's a recommendit habit to use brackets, even if you have only one statement after if. perhaps later while the maintance you come back to this if and add an other statement and the bug is there. but not if ... ALWAYS USE BRACKETS.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    it's a recommendit habit to use brackets, even if you have only one statement after if. perhaps later while the maintance you come back to this if and add an other statement and the bug is there. but not if ... ALWAYS USE BRACKETS.
    Lots of IDEs recommended this feature too. NetBeans is one of them. :D

    Actually those things are good habit in coding, in any language. Then the code is much clear. As far as I aware, most of the text books highlighted those things in many examples and so on.

Similar Threads

  1. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  2. Looping if statements
    By rice in forum New To Java
    Replies: 10
    Last Post: 10-02-2009, 02:50 AM
  3. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  4. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 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
  •