Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You have if(choice == 'A') that is giving trouble you. Also check another braces that are not correct indented. It's difficult to read. Code should be readable by human beings

See:

if (choice == 'A')// this is giving trouble to you
if (hours <= 10) { 
  .
  .

This code is equivalent to,(braces added)

if (choice == 'A'){
    if (hours <= 10) {
     .
     . 
}

So you have to remove it. What you want to do to be more clear is this. You can use switch

switch(choice){
 case 'A':
       System.out.println ("Your total charges are: " + (hours <=10) ?chargesa:extrafeesA);
       break;
 case 'B':
       System.out.println ("Your total charges are: " + (hours <=10) ?chargesB:extrafeesB);
       break;
 case 'C':
       System.out.println ("Your total charges are: " + chargesc);
       break; 
 }

Try to use braces correctly :)

if (choice == 'A') {
   if (hours <= 10) 
      System.out.println ("Your total charges are: " + chargesa);
   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);
   if (hours > 20)
      System.out.println ("Your total charges are: " + extrafeesB);
}
else if (choice == 'C')
   System.out.println ("Your total charges are: " + chargesc);

You could simply use 'AND' in order to get your conditions work correctly.

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

change this:

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

to

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

From looking at it,

if (choice == 'A')
if (hours <= 10) { 

The two are not indented, meaning if (hours <= 10) { will run regardless of the outcome of the first statement. So when the user types in B, and then the hours (less than 10), the first statement reads false, and the second reads true, therefore running that one.

Try putting {} around all the if statements and try running it again.

I may be incorrect though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top