If else statements not working- why?

Dingles picture Dingles · Oct 3, 2013 · Viewed 46.6k times · Source

After working on this code for a while and coming very close to the end, I've run into an unexpected problem. The if and else statements for choices 'B' and 'C' display nothing on the screen when they are typed in as user input. I've tried a few things but I cannot seem to figure it out.

import java.util.Scanner;
public class Internet_Service_Provider
{
    public static void main(String[] args)
    {
        double hours;
        char choice;

        Scanner input = new Scanner(System.in);

        System.out.println ("Enter the letter of your package: ");
        choice = input.nextLine().charAt(0);

        System.out.println ("Enter the number of hours used: ");
        hours = input.nextDouble();

        double chargesa, chargesb, chargesc;
        chargesa = 9.95;
        chargesb = 13.95;
        chargesc = 19.95;

        double chargesa2, chargesb2;
        chargesa2 = (hours - 10) * 2;
        chargesb2 = (hours - 20);

        double extrafeesA, extrafeesB;
        extrafeesA = chargesa + chargesa2;
        extrafeesB = chargesb + chargesb2;

        if (choice == 'A')
            if (hours <= 10) { 
                System.out.println ("Your total charges are: " + chargesa);
            }
        else if (choice == 'A')
            if (hours > 10){
                 System.out.println ("your total charges are: " + extrafeesA);
             }
        else if (choice == 'B')
            if (hours <= 20) {
                 System.out.println ("Your total charges are: " + chargesb);
            }
        else if (choice == 'B')
            if (hours > 20){
                 System.out.println ("Your total charges are: " + extrafeesB);
            }
        else if (choice == 'C'){
             System.out.println ("Your total charges are: " + chargesc);
        }
    }
}

When the user types 'A' and then types the hours, the program runs perfectly and gives me the desired output. If typing 'B' or 'C' and then the hours, there is no output onto the screen. What is causing this?

-EDIT- I messed around with the code before checking the responses and discovered that if removing the else and creating this code:

  if (choice == 'A')
        if (hours <= 10) { 
              System.out.println ("Your total charges are: " + chargesa);
        }

  if (choice == 'A')
        if (hours > 10){
              System.out.println ("your total charges are: " + extrafeesA);
        }

  if (choice == 'B')
        if (hours <= 20) {
              System.out.println ("Your total charges are: " + chargesb);
        }

  if (choice == 'B')
        if (hours > 20){
              System.out.println ("Your total charges are: " + extrafeesB);
        }

  if (choice == 'C'){
        System.out.println ("Your total charges are: " + chargesc);}

... the program runs properly. I guess the else statements were unnecessary and caused the problem.

Answer

3yakuya picture 3yakuya · Oct 3, 2013
if (choice == 'A'){
    if (hours <= 10) { 
      System.out.println ("Your total charges are: " + chargesa);}
    else if (choice == 'A')
      if (hours > 10){
      System.out.println ("your total charges are: " + extrafeesA);}
    else if (choice == 'B')
      if (hours <= 20) {
      System.out.println ("Your total charges are: " + chargesb);}
    else if (choice == 'B')
      if (hours > 20){
      System.out.println ("Your total charges are: " + extrafeesB);}
    else if (choice == 'C'){
      System.out.println ("Your total charges are: " + chargesc);}
    }
}

Just adding some proper tabulation reveals, that you do not actually cover any other case than "A". Tabulation makes the code look much clearer, and therefore it is easier to find an error. You need to use your braces properly, as for now you only check if choice == 'A'. If it does not - your code doesn't do anything.