0

I have two classes for calculating the cost of an ISP plan and gathering input/displaying the cost. ISPMain.java and ISP.java ISPMain is supposed to display service plans and ask the user to select their plan by the letter of the package, like package a, package b, or package c. I have it set up so the user inputs the character to choose the plan. If the plan is the not package c (unlimited plan) the user is prompted to enter the hours they used.

Once the hours are entered and stored in hoursUsed, ISP.java calculates the cost and then ISPMain.java displays it. My problem is that my switch statement in ISP only is displaying the default value and I am not sure why. Can anyone explain?

public class ISPMain
{

    public static void main(String[] args)
    {
        char pkg;
        double hoursUsed; 

        Scanner kb = new Scanner(System.in);

        System.out.println("The three packages offered are: ");
        System.out.println("Package A: For $9.95 per month, 10 hours of access are provided. \nAdditional hours are $2.00 per hour.");
        System.out.println("Package B: For $14.95 per month, 20 hours of access are provided. \nAdditional hours are $1.00 per hour.");
        System.out.println("Package C: For $19.95 per month, unlimited access is provided.");

        System.out.println("Please type the letter of the package you have: ");
        pkg = kb.nextLine().toUpperCase().charAt(0);

        if(pkg == 'A')
        {
            System.out.print("Enter number of hours: ");
            hoursUsed = kb.nextDouble();
        }
        else if(pkg == 'B')
        {
            System.out.print("Enter number of hours: ");
            hoursUsed = kb.nextDouble();
        }
        else if(pkg == 'C')
        {
            System.out.print("You have unlimited access! No need to enter hours used. \n");
        }


        ISP user = new ISP();  
        System.out.print("Total charges: " + user.calculateCharges());

    }
}

switch statement from ISP.java:

public double calculateCharges()
{

    switch (pkg)
    {
        case 'A':
        if (hoursUsed < 10)
        {
            return 9.95;
        }

        else
        {
            return (hoursUsed - 10)*2 + 9.95;
        }

        case 'B':
        if (hoursUsed < 20)
        {
            return 14.95;
        }

        else
        {
            return (hoursUsed - 20) + 14.95;
        }  

        case 'C':
            return 19.95;

        default:
            System.out.println("Invalid input!");
            return 0;
    }


}

to conclude, my if else works fine, but the switch only displays "Invalid input"

4
  • what input is passed in pkg variable Commented Mar 20, 2017 at 17:45
  • 1
    You declare the pkg variable in the scope of the main method. You can't access that variable inside the calculateCharges method without either passing it or creating pkg as a class member. Commented Mar 20, 2017 at 17:45
  • 1
    ISPMain.pkg and ISP.pkg are TOTALLY different variables, where are you connecting those?? Commented Mar 20, 2017 at 17:49
  • Hard to believe that the "Java switch statement" is not working correctly, have you tried to print the value of pkg just before the switch? System.out.println("pkg: " + pkg"); switch (pkg) { Commented Mar 20, 2017 at 17:50

2 Answers 2

1

As others have said, you need to get the pkg variable into the scope of your switch statement. Try this:

public double calculateCharges(char pkg){
 switch(pkg){
     case('A'):{
      //insert code here
       break;
     }
     case('B'):{
    //insert code here
     break;
     }
     case('C'):{
     //insert more code here
     break;
     }
  default:{
  //code here 
  break;
  }
 }
}

Then call your method like:

System.out.print("Total charges: " + user.calculateCharges(pkg));
Sign up to request clarification or add additional context in comments.

3 Comments

Side note: you don't really need all those extra curly braces.
The ones after each case? Yeah I wasn't too sure about that. Thanks.
Don't get me wrong - there are use cases for those curly braces, but yours wasn't one of them. For example, one might wish to declare a variable local to a specific case, and that would require a case-specific block.
1

The default: case statement will always run in your code because char pkg isn't defined as a parameter for your method.

The user may enter A, B, or C, but since it does not get passed the end result will always be default, therefore causing your Invalid input.

Try :

public double calculateCharges(char pkg){
    switch (pkg)
    {
       case 'A':
       if (hoursUsed < 10){
        return 9.95;
       }else{
        return (hoursUsed - 10)*2 + 9.95;
       }

       case 'B':
       if (hoursUsed < 20){
        return 14.95;
       }else{
        return (hoursUsed - 20) + 14.95;
       }  

       case 'C':
       return 19.95;

       default:
       System.out.println("Invalid input!");
       return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.