Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Help with Java Program!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Java Program!

    Hi everyone, first time entry. I have a problem with my java program output. It looks like the program logic is correct, but there are still some line of statements that are executed that I do not want.

    Here's the description of the problem:

    An Internet service provider has 3 different subscription packages for its customers:

    Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
    Package B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
    Package C: For $19.95 per month unlimited access is provided.

    Write a program that calculates a customer's monthly bill. It should ask the user to enter the letter of the package the customer has purchased (A, B, or C) and the number of hours that were used. It should then display the total charges. If the package is not valid, or if the number of hours is greater than 744, display an error message.
    And here's my java source code:
    import java.util.Scanner;
     
    public class internetPackage
    {
      public static void main(String[] args)
      {
        String input;
        char servicePackage;
        int hoursUsed, regularHours, extraHours;
        double monthlyFee, totalFee, extraHoursFee;
     
        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
     
        // Prompt user to choose Internet Service Package.
        System.out.print("Internet Service Provider Monthly Bill\n");
     
        System.out.print("\nEnter package type (A,B, or C):  ");
        input = keyboard.nextLine();
        servicePackage = input.charAt(0);
     
        // Prompt user to enter number of hours.
        System.out.print("Enter number of hours:  ");
        hoursUsed = keyboard.nextInt();
     
        if (hoursUsed < 0)
          System.out.println("Invalid input. Hours cannot be negative.");
     
        switch (servicePackage)
        {
           case 'a':
           case 'A':
               monthlyFee = 9.95;
               regularHours = 10;
               if (hoursUsed < regularHours && hoursUsed >=  0)
               {
                 totalFee = monthlyFee;
                 System.out.println("Total due:  $" + totalFee);
                 break;
               }
               else
               {
                 extraHours = hoursUsed - regularHours;
                 extraHoursFee = extraHours * 2.00;
                 totalFee = monthlyFee + extraHoursFee;
                 System.out.println("Total due:  $" + totalFee);
                 break;
               }
           case 'b':
           case 'B':
               monthlyFee = 13.95;
               regularHours = 20;
               if (hoursUsed < regularHours)
               {
                 totalFee = monthlyFee;
                 System.out.println("Total due:  $" + totalFee);
                 break;
               }
               else
               {
                 extraHours = hoursUsed - regularHours;
                 extraHoursFee = extraHours * 1.00;
                 totalFee = monthlyFee + extraHoursFee;
                 System.out.println("Total due:  $" + totalFee);
                 break;
               }
           case 'c':
           case 'C':
               monthlyFee = 19.95;
               if (hoursUsed < 744)
               {
                 totalFee = monthlyFee;
                 System.out.println("Total due:  $" + totalFee);
                 break;
               }
               else
               {
                 System.out.println("Invalid input. Hours cannot exceed 744.");
                 break;
               }
           default:
                 System.out.println("Invalid package type");
        }     
      } 
    }

    Rules:
    1) If the user inputs a negative number, the program should automatically display: "Invalid input. Hours cannot be negative."
    2) If other letters (except A, B, and C) are inputted, the program should automatically display: "Invalid package type".
    And here are some sample outputs. The idea is to show results when choosing A, B, or C.:
    Enter package type (A,B, or C): A
    Enter number of hours: -1
    Invalid input. Hours cannot be negative.
    Total due: $-12.05 <--THIS SHOULD NOT SHOW UP, BUT IT STILL DOES

    Enter package type (A,B, or C): D
    Enter number of hours: 2 <--THIS SHOULD NOT SHOW UP, BUT IT STILL DOES
    Invalid package type
    Much appreciated for any help I can get. Thanks for reading!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    Enter package type (A,B, or C): D
    Enter number of hours: 2 <--THIS SHOULD NOT SHOW UP, BUT IT STILL DOES
    Invalid package type
    The code will be executed in the order that it is in. The question: Enter number of hours:
    is asked BEFORE the package type is tested.

    Enter package type (A,B, or C): A
    Enter number of hours: -1
    Invalid input. Hours cannot be negative.
    Total due: $-12.05 <--THIS SHOULD NOT SHOW UP, BUT IT STILL DOES
    If the input is invalid, put out a message and SKIP printing out the Total. An else statement can be used to surround the logic that prints the total.


    BTW you should always use {}s after if statements to enclose the block of code in the if.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    Okay, I was able to resolve the "Invalid input. Hours cannot be negative" by placing if (hoursUsed < 0) System.out.println("Invalid input. Hours cannot be negative."); into each switch.

    I'm still having issues with when a user selects another letter other than A, B, or C. It still asks me "How many hours used?" then goes to "Invalid package type."

    I'm really stumped. I worked on this for a day and half and can't get this part resolved..

    --- Update ---

    Quote Originally Posted by Norm View Post
    The code will be executed in the order that it is in. The question: Enter number of hours:
    is asked BEFORE the package type is tested.


    If the input is invalid, put out a message and SKIP printing out the Total. An else statement can be used to surround the logic that prints the total.


    BTW you should always use {}s after if statements to enclose the block of code in the if.
    The sample program run that the professor expects us to output:

    Internet Service Provider Monthly Bill

    Enter package type (A, B, or C): D
    Invalid package type
    So, my logic is if a user enters "D," it'll skip all the options (A, B, and C), and goes directly to the else statement "Invalid package type"

    Am I not catching the mistake here?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    goes directly to the else statement
    I don't see an else statement in the code where you are talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    Quote Originally Posted by Norm View Post
    I don't see an else statement in the code where you are talking about.
    Whoops, my bad. It's under "Default." Towards the end of the program.

    For Switch Statements, "Default" acts like an "Else" incase the conditions being tested are all false, right?

           case 'c':
           case 'C':
               monthlyFee = 19.95;
               if (hoursUsed < 744)
               {
                 totalFee = monthlyFee;
                 System.out.println("Total due:  $" + totalFee);
                 break;
               }
               else
               {
                 System.out.println("Invalid input. Hours cannot exceed 744.");
                 break;
               }
           default:
                 System.out.println("Invalid package type");
        }     
      } 
    }
    Last edited by javaBoi; March 19th, 2014 at 10:32 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    For Switch Statements, "Default" acts like an "Else" incase the conditions being tested are all false, right?
    Yes.
    But isn't the switch statement AFTER where the "Invalid input. Hours cannot be negative." statement is printed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    Quote Originally Posted by Norm View Post
    Yes.
    But isn't the switch statement AFTER where the "Invalid input. Hours cannot be negative." statement is printed?
    Oh, sorry. In my previous post, I actually mentioned that I took that "Invalid input. Hours cannot be negative." statement from it's original position and placed it as an ELSE statement for each switch case.

    My issue is still where a user enter in another String letter (in this case, 'D'), and I want it to bypass everything else to get this output:
    Enter package type (A,B, or C): D
    Invalid package type
    And not this:
    Enter package type (A,B, or C): D
    Enter number of hours: 2 <--THIS SHOULD NOT SHOW UP, BUT IT STILL DOES
    Invalid package type
    I checked the order in which the computer will read it, but I think it should still work (obviously it didn't, though) because the conditions were all false...But, it's still asking me "Enter the number of hours." Ahh, getting frustrated with this..

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    Something like this?
    get user response
    if not one of : A, B, C >> print an error message
    ELSE get the number of hours and process the valid response
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    I wanted to do that, but how would I write it?

    Would this work?

    If (servicePackage != 'A' && servicePackage != 'a' && servicePackage != 'B' && servicePackage != 'b' && servicePackage != 'C' && servicePackage != 'c')
    System.out.println("Invalid service type");

    THEN the else statement will go into the switch statements?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    Would this work?
    Try it and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    Nope, still gives me the same output, actually..

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    Can you post the new code that shows the problem?
    and the console from when you execute it.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    import java.util.Scanner;
     
    public class internetPackage
    {
      public static void main(String[] args)
      {
        String input;
        char servicePackage;
        int hoursUsed, regularHours, extraHours;
        double monthlyFee, totalFee, extraHoursFee;
     
        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
     
        // Prompt user to choose Internet Service Package.
        System.out.print("Internet Service Provider Monthly Bill\n");
     
        System.out.print("\nEnter package type (A, B, or C):  ");
        input = keyboard.nextLine();
        servicePackage = input.charAt(0);
     
        // Prompt user to enter number of hours.
        System.out.print("Enter number of hours:  ");
        hoursUsed = keyboard.nextInt();
     
        if (servicePackage != 'A' && servicePackage != 'a' && servicePackage != 'B' && servicePackage != 'b' && servicePackage != 'C' && servicePackage != 'c')
        {
          System.out.println("Invalid service package");
        }
        else 
        {
          switch (servicePackage)
          {
             case 'a':
             case 'A':
                 monthlyFee = 9.95;
                 regularHours = 10;
                 if (hoursUsed < 0)
                 {
                   System.out.println("Invalid input. Hours cannot be negative.");
                   break;
                 }
                 else  if (hoursUsed < regularHours && hoursUsed >=  0)
                 {
                   totalFee = monthlyFee;
                   System.out.println("Total due:  $" + totalFee);
                   break;
                 }
                 else
                 {
                   extraHours = hoursUsed - regularHours;
                   extraHoursFee = extraHours * 2.00;
                   totalFee = monthlyFee + extraHoursFee;
                   System.out.println("Total due:  $" + totalFee);
                   break;
                 }
             case 'b':
             case 'B':
                 monthlyFee = 13.95;
                 regularHours = 20;
                 if (hoursUsed < 0)
                { 
                   System.out.println("Invalid input. Hours cannot be negative.");
                   break;
                 }
               else if (hoursUsed < regularHours)
                 {
                   totalFee = monthlyFee;
                   System.out.println("Total due:  $" + totalFee);
                   break;
                 }
                 else
                 {
                   extraHours = hoursUsed - regularHours;
                   extraHoursFee = extraHours * 1.00;
                   totalFee = monthlyFee + extraHoursFee;
                   System.out.println("Total due:  $" + totalFee);
                   break;
                 }
             case 'c':
             case 'C':
                 monthlyFee = 19.95;
                 if (hoursUsed < 0)
                 {
                   System.out.println("Invalid input. Hours cannot be negative.");
                   break;
                 }
                 else if (hoursUsed < 744)
                 {
                   totalFee = monthlyFee;
                   System.out.println("Total due:  $" + totalFee);
                   break;
                 }
                 else
                 {
                   System.out.println("Invalid input. Hours cannot exceed 744.");
                   break;
                 }
             default:
                   System.out.println("Invalid package type");
     
          }
          }     
        } 
      }

    OUTPUTS:
    Internet Service Provider Monthly Bill

    Enter package type (A, B, or C): A
    Enter number of hours: 3
    Total due: $9.95


    Internet Service Provider Monthly Bill

    Enter package type (A, B, or C): A
    Enter number of hours: -1
    Invalid input. Hours cannot be negative.


    Internet Service Provider Monthly Bill

    Enter package type (A, B, or C): A
    Enter number of hours: 20
    Total due: $29.95


    Internet Service Provider Monthly Bill

    Enter package type (A, B, or C): D
    Enter number of hours: 3
    Invalid service package
    First 3 output are right, but the 3rd one is still incorrect.
    Last edited by javaBoi; March 19th, 2014 at 12:17 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    The question about the hours is asked BEFORE the test of package type.
    In post#8 I suggested that the test for package type be made BEFORE the hours are requested.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    Quote Originally Posted by Norm View Post
    The question about the hours is asked BEFORE the test of package type.
    In post#8 I suggested that the test for package type be made BEFORE the hours are requested.
    It is, right? The program does ask for the service package first, then the number of hours used..Or is the program written in the wrong order in terms of execution of the statements?

    public class internetPackage
    {
      public static void main(String[] args)
      {
        String input;
        char servicePackage;
        int hoursUsed, regularHours, extraHours;
        double monthlyFee, totalFee, extraHoursFee;
     
        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
     
        // Prompt user to choose Internet Service Package.
        System.out.print("Internet Service Provider Monthly Bill\n");
     
        System.out.print("\nEnter package type (A, B, or C):  ");
        input = keyboard.nextLine();
        servicePackage = input.charAt(0);
     
        // Prompt user to enter number of hours.
        System.out.print("Enter number of hours:  ");
        hoursUsed = keyboard.nextInt();

    The way the code is written:
    1) If the user enters a letter other than A,B or C and the first line of code will execute: if (servicePackage != 'A' && servicePackage != 'a' and so on).
    2) In this case, the user enters 'D.' So, that condition is would be true since D is not equal to A, a, B, b, C, and c. So, the statement "Invalid service package" would display.
    3) If the user enters 'A," then the condition would be false because A is actually equal to A or a.
    4) Then it prompts to the switch statements regarding the number of hours used.

    Does that sound right? I appreciate the help you're providing me, by the way.

    --- Update ---

    OMG, JUST FIGURED IT OUT! Didn't understand your suggestion, but had it working, anyways. Thanks for your suggestions, though.
    Last edited by javaBoi; March 19th, 2014 at 12:41 PM.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    I thought that you did NOT want the question: "Enter number of hours: " asked BEFORE the test for valid package type was made.
    The posted code always asks the question without any test of package type before asking.

    Your list of 4 steps doesn't mention the question: "Enter number of hours: "
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    Oh no, I needed the question in program (but not in the place I initially had it). Sorry for not being clear from the start.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with Java Program!

    Have you solved the problem now?

    If not, please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Program!

    Quote Originally Posted by Norm View Post
    Have you solved the problem now?

    If not, please explain.
    Yeah, the program works perfectly fine now.

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 06:16 AM
  2. convert java program to java ME program
    By abhishek1212 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 02:22 PM
  3. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 04:16 AM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM

Tags for this Thread