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 10 of 10

Thread: Java Homework Help

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Java Homework Help

    This is the guidelines for the program I'm making:

    An Internet service provider has three 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 (integer) that were used. It should then display the total charges. If an invalid package (other than A, B, or C) or invalid number of hours (<0 or > 31*24) is input, the program should display an appropriate error message and stop. Use if statements for input validation and use a switch statement to calculate total charges depending on the package the customer selected.

    This is what I have in my program:
    import java.util.Scanner;
    public class InternetServiceProviderPart12
    {
    public static void main(String[] args)
    {
    char package;
    int hours;
    int packageHours;
    int addedHours;
    double monthlyCharge;
    double addedHoursCharge;
    double total;
    Scanner kb = new Scanner(System.in);
    System.out.print("Enter the customer's package (A, B, or C): ");
    char package = kb.next().charAt(0);
    kb.nextLine();
    if (package != 'A'||package != 'B'||package != 'C')
    {
    System.exit(0);
    }
    System.out.print("Enter the number of hours used: ");
    int hours = kb.nextInt();
    kb.nextLine();
    if (hours < 0||hours > 31 * 24)
    {
    System.exit(0);
    } switch(package){
    case 'A':
    monthlyCharge = 9.95;
    packageHours = 10;
    addedHours = packageHours - hours;
    addedHoursCharge = addedHours * 2.00;
    total = monthlyCharge + addedHoursCharge;
    System.out.println("The charges are $" + total);
    break;
    case'B': monthlyCharge = 13.95;
    packageHours = 20;
    addedHours = packageHours - hours;
    addedHoursCharge = addedHours * 2.00;
    total = monthlyCharge + addedHoursCharge;
    System.out.println("The charges are $" + total);
    break;
    case 'C':
    monthlyCharge = 19.95;
    System.out.println("The charges are $" + total);
    break;
    default:
    System.out.print("Invalid");
    System.exit(0);
    }
    }
    }



    I keep getting these errors when compiling:
    InternetServiceProviderPart12.java:8: not a statement
    char package;
    ^
    InternetServiceProviderPart12.java:8: ';' expected
    char package;
    ^
    InternetServiceProviderPart12.java:17: not a statement
    char package = kb.next().charAt(0);
    ^
    InternetServiceProviderPart12.java:17: ';' expected
    char package = kb.next().charAt(0);
    ^
    InternetServiceProviderPart12.java:19: illegal start of expression
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: illegal start of expression
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: ';' expected
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: illegal start of expression
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: ';' expected
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: illegal start of expression
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: ';' expected
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: illegal start of expression
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: ';' expected
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: illegal start of expression
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: ';' expected
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: illegal start of expression
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:19: ';' expected
    if (package != 'A'||package != 'B'||package != 'C')
    ^
    InternetServiceProviderPart12.java:23: <identifier> expected
    System.out.print("Enter the number of hours used: ");
    ^
    InternetServiceProviderPart12.java:23: illegal start of type
    System.out.print("Enter the number of hours used: ");
    ^
    InternetServiceProviderPart12.java:25: <identifier> expected
    kb.nextLine();
    ^
    InternetServiceProviderPart12.java:26: illegal start of type
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: illegal start of type
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: <identifier> expected
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: ')' expected
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: ';' expected
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: illegal start of type
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: <identifier> expected
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: ';' expected
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: illegal start of type
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:26: <identifier> expected
    if (hours < 0||hours > 31 * 24)
    ^
    InternetServiceProviderPart12.java:27: ';' expected
    {
    ^
    InternetServiceProviderPart12.java:28: illegal start of type
    System.exit(0);
    ^
    InternetServiceProviderPart12.java:28: illegal start of type
    System.exit(0);
    ^
    InternetServiceProviderPart12.java:30: class, interface, or enum expected
    switch(package){
    ^
    InternetServiceProviderPart12.java:33: class, interface, or enum expected
    packageHours = 10;
    ^
    InternetServiceProviderPart12.java:34: class, interface, or enum expected
    addedHours = packageHours - hours;
    ^
    InternetServiceProviderPart12.java:35: class, interface, or enum expected
    addedHoursCharge = addedHours * 2.00;
    ^
    InternetServiceProviderPart12.java:36: class, interface, or enum expected
    total = monthlyCharge + addedHoursCharge;
    ^
    InternetServiceProviderPart12.java:37: class, interface, or enum expected
    System.out.println("The charges are $" + total);
    ^
    InternetServiceProviderPart12.java:38: class, interface, or enum expected
    break;
    ^
    InternetServiceProviderPart12.java:39: class, interface, or enum expected
    case'B':
    ^
    InternetServiceProviderPart12.java:41: class, interface, or enum expected
    packageHours = 20;
    ^
    InternetServiceProviderPart12.java:42: class, interface, or enum expected
    addedHours = packageHours - hours;
    ^
    InternetServiceProviderPart12.java:43: class, interface, or enum expected
    addedHoursCharge = addedHours * 2.00;
    ^
    InternetServiceProviderPart12.java:44: class, interface, or enum expected
    total = monthlyCharge + addedHoursCharge;
    ^
    InternetServiceProviderPart12.java:45: class, interface, or enum expected
    System.out.println("The charges are $" + total);
    ^
    InternetServiceProviderPart12.java:46: class, interface, or enum expected
    break;
    ^
    InternetServiceProviderPart12.java:47: class, interface, or enum expected
    case 'C':
    ^
    InternetServiceProviderPart12.java:49: class, interface, or enum expected
    System.out.println("The charges are $" + total);
    ^
    InternetServiceProviderPart12.java:50: class, interface, or enum expected
    break;
    ^
    InternetServiceProviderPart12.java:51: class, interface, or enum expected
    default:
    ^
    InternetServiceProviderPart12.java:53: class, interface, or enum expected
    System.exit(0);
    ^
    InternetServiceProviderPart12.java:54: class, interface, or enum expected
    }
    ^

    I know it's is a lot of errors and I know how to reduce it to 13 errors. I'm in quite a hurry and need to finish it by tomorrow. All I really care about on fixing is the errors for char, using package, and the switch. Please help. Thank-you.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Homework Help

    Please read the Announcement topic at the top of the sub-forum to learn how to post your code properly along with other useful stuff new users should know.

    'package' is a Java keyword and cannot be used as a variable name.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Homework Help

    Quote Originally Posted by GregBrannon View Post
    Please read the Announcement topic at the top of the sub-forum to learn how to post your code properly along with other useful stuff new users should know.

    'package' is a Java keyword and cannot be used as a variable name.

    Thank you so much!! that reduced all my errors to only one error! you're awesome!

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java Programming Homework Help!!

    This is the guidelines for the program I'm making:

    An Internet service provider has three 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 (integer) that were used. It should then display the total charges. If an invalid package (other than A, B, or C) or invalid number of hours (<0 or > 31*24) is input, the program should display an appropriate error message and stop. Use if statements for input validation and use a switch statement to calculate total charges depending on the package the customer selected.

    This is what I have in my program:

    import java.util.Scanner;
    public class InternetServiceProviderPart1
    {
       public static void main(String[] args)
       {
       int packageHours;
       int addedHours;   
       double monthlyCharge;
       double addedHoursCharge;
       double total;
       Scanner kb = new Scanner(System.in);
       System.out.print("Enter the customer's package (A, B, or C): ");
       char pkg = kb.next().charAt(0);
       kb.nextLine();
     
      if ((pkg != 'A') || (pkg != 'B') || (pkg != 'C'))
          {
          System.exit(0);
          }
       System.out.print("Enter the number of hours used: ");
       int hour = kb.nextInt();
       kb.nextLine();
       if ((hour < 0) || (hour > 31 * 24))
          {
          System.exit(0);
         }
       switch(pkg){
          case 'A':
             monthlyCharge = 9.95;
             packageHours = 10;
             addedHours = packageHours - hour;
             addedHoursCharge = addedHours * 2.00;
             total = monthlyCharge + addedHoursCharge;
             System.out.println("The charges are $" + total);
             break;
     
         case'B':
             monthlyCharge = 13.95;
             packageHours = 20;
             addedHours = packageHours - hour;
             addedHoursCharge = addedHours * 2.00;
             total = monthlyCharge + addedHoursCharge;
             System.out.println("The charges are $" + total);
             break;    
             case 'C':
             monthlyCharge = 19.95;
             System.out.println("The charges are $" + monthlyCharge);
             break;
          default:
             System.out.print("Invalid");
             System.exit(0);
          }
       }
    }

    I get no errors when compiling the program however when I execute it, as soon as I input the package, the program terminates. PLease help?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Homework Help

    You're welcome.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Programming Homework Help!!

    Please don't start a new thread for the same topic. Continue working the problems in the original post.

    And learn to post your code in code tags as I asked in the original thread. If you don't, your posts will be ignored by most.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Programming Homework Help!!

    Alright thanks and sorry I'm new to this so lets see. Hope this helps I placed code tags.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Homework Help

    When you do something as dramatic as System.exit( 0 ) in response to a user input error, you should write a short line of output to describe what's going on. Something like the following right before the system exit command:

    System.out.println( "Exiting the program because the package you entered is not valid." );

    Then, when you find out the program is exiting no matter what the user enters, add another print statement to figure out what is being evaluated by the if statement that exits the program. A statement like:

    System.out.println( "The pkg variable is: " + pkg );

    Hmmmm. Think about those ORd statements. Say it differently. "If this is false OR that is false OR the other thing is false, exit the program." Will that ever be true so that the program does not exit? What needs to change?

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Homework Help

    Alright everything worked well. I tweaked just a bit more of the coding that messed up the prices. Now it works perfectly. Thank-you so much Greg!

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Homework Help

    Again, you're welcome. Glad to help.

Similar Threads

  1. Java Homework
    By Dimo62 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 8th, 2013, 11:57 AM
  2. Replies: 8
    Last Post: February 12th, 2013, 05:45 AM
  3. New to Java. Need help with homework
    By ptison in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2013, 05:53 PM
  4. JAVA HOMEWORK HELP!
    By javafirsttime in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 14th, 2011, 07:00 PM
  5. MORE java homework help.
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: October 13th, 2009, 07:15 PM