Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JavaHelp

Options
  • 16-10-2013 10:15pm
    #1
    Registered Users Posts: 20


    I have been getting an error that says I have an else without an if and I can't figure out why. I was wondering if someone could have a look and try to help me . It's probably just something simple but I am only starting to learn so am unable to find the problem myself. Any help would be appreciated :)

    Here's the code I'm having trouble with.

    if ((totalPrice >= 20.00) && (totalPrice < 40.00)) {
    discountAmount = totalPrice*DISCOUNT1;
    netPrice = totalPrice - discountAmount;
    System.out.printf("You have recieved a discount value of %s% yielding a discount of %S£", DISCOUNT1,discountAmount);
    System.out.printf("Your final cost after the discount has been applied is %s£" , netPrice);
    }
    else if (totalPrice >= 40.00)
    discountAmount = totalPrice*DISCOUNT1;
    netPrice = totalPrice - discountAmount;
    System.out.printf("You have recieved a discount value of %s% yielding a discount of %S£", DISCOUNT2,discountAmount);
    System.out.printf("Your final cost after the discount has been applied is %s£" , netPrice);

    else {
    System.out.printf("Your total has come to %s£", totalPrice);
    }
    Tagged:


Comments

  • Registered Users Posts: 2,089 ✭✭✭henryporter


    Missing a curly bracket at the end of the else if block - just before the else


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    I wonder if your lecturer reads this forum...


  • Registered Users Posts: 2,089 ✭✭✭henryporter


    I know :rolleyes: still have to learn somewhere and it sounds like these lecturers aren't doing too much teaching


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    I know :rolleyes: still have to learn somewhere and it sounds like these lecturers aren't doing too much teaching

    Or they are not studying.... you really shouldnt be giving the answers straight up.

    Help someone sure, walk them through, question their line of thought, but feeding them answers hurts everyone.


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    Missing a curly bracket at the end of the else if block - just before the else

    And at the start! :pac:
    if ((totalPrice >= 20.00) && (totalPrice < 40.00)) {
    
    }
    else if (totalPrice >= 40.00) [B][SIZE="6"]{[/SIZE][/B]
    
    [B][SIZE="6"]}[/SIZE][/B]
    
    else {
    
    }
    


  • Advertisement
  • Registered Users Posts: 20 lkavo


    Thanks! We were told that we didn't need to put { on else ifs, I have other else ifs in the programme without any { in them and they work fine, so without someone telling me I probably would never have figured it out.


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    If there is only one line after the (else) if, you can omit the curly braces, but if there are more than one, you will need to wrap them in curly braces.
    /* In this first example, both lines print if x equals y */
    if(x==y){
        System.out.println("Hello!"); 
        System.out.println("Goodbye!");
    }
    
    /* In this example, first line gets printed if x equals y */
    /* The second line gets printed always as it's not part of the if statement */
    if(x==y)
        System.out.println("Hello!"); 
        System.out.println("Goodbye!");
    

    I would recommend to always use the braces even if it's only one line inside.


  • Registered Users Posts: 4,358 ✭✭✭robbiezero


    lkavo wrote: »
    Thanks! We were told that we didn't need to put { on else ifs, I have other else ifs in the programme without any { in them and they work fine, so without someone telling me I probably would never have figured it out.

    Its generally good practice to always use {} on if-else statements even though they are not necessary if the if-else only contain a single statement.


Advertisement