Results 1 to 9 of 9
  1. #1
    twyzard is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Java code problem with three different internet service provider packages.

    What to do with this JAVA code?
    This is the code that I need to do without using the Joptionpane:
    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 that were used. It should then display the total charges.

    This is what I have so far : Cannot use Joptionpaneshowinputdialog





    /* A demonstration of how to use Decision Structures
    * Taha Ahmed
    * Cop2250-U03 Project_2
    * #13 on page 174 of the textbook
    *
    */

    import java.util.Scanner;
    /**
    This program demonstrates a switch statement.
    */
    public class ISP13
    {
    public static void main(String[] args)
    {
    char packageLetter;
    int hoursUsed;
    int regularHours;
    int additionalHours;
    double monthlyFee;
    double additionalHoursFee;
    double totalFee;
    String input;

    Scanner keyboard = new Scanner(System.in);

    input = input.toUpperCase();
    packageLetter = input.charAt(0);

    System.out.println("Enter the letter of the " +
    "package you purchased (either A, B, or C.");


    System.out.println("Enter the number of hours " +
    "you used.");
    hoursUsed = Integer.parseInt(input);

    switch(packageLetter)
    {
    case 'A':
    monthlyFee = 9.95;
    regularHours = 10;
    additionalHours = hoursUsed - regularHours;
    additionalHoursFee = additionalHours * 2.00;
    totalFee = monthlyFee + additionalHoursFee;
    System.out.println("The total charges is $" +
    totalFee + ".");
    break;
    case 'B':
    monthlyFee = 13.95;
    regularHours = 20;
    additionalHours = hoursUsed - regularHours;
    additionalHoursFee = additionalHours * 1.00;
    totalFee = monthlyFee + additionalHoursFee;
    System.out.println("The total charges is " + totalFee);
    System.out.println("The total charges is $" +
    totalFee + ".");
    break;
    case 'C':
    totalFee = 19.95;
    System.out.println("The total charges is $" +
    totalFee + ".");
    break;
    default:
    System.out.println("Plea… enter either A,B, " +
    "or C).");
    }
    System.exit(0);

    }

    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    You have declared and initialised a Scanner, presumably because you want to get user input, but then you neglected to use it anywhere in your code :confused:

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Problem here:

    Java Code:
    hoursUsed = Integer.parseInt(input);
    input = keyboard.nextLine();
    hoursUsed = input.charAt(0);
    input cannot be parsed to an int because it holds a String which does not contain all numerical characters e.g. "93759" it holds whatever package you choose i.e. 'A' or 'B' or something. (NumberFormatException)

  4. #4
    twyzard is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    ok can you tell me how to fix this code please

  5. #5
    Dr.Fox is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    System.out.println("Enter the number of hours " +
    "you used.");
    hoursUsed = Integer.parseInt(input);
    input = keyboard.nextLine();
    hoursUsed = input.charAt(0);
    Why are you parsing your input before reading the users input of hours? This will make the program try to parse the last input, the letter A, B or C they typed in before.

    Also you dont want to make hoursUsed a char.

    try:
    Java Code:
    System.out.println("Enter the number of hours " +
    "you used.");
    input = keyboard.nextLine();
    hoursUsed = Integer.parseInt(input);

  6. #6
    twyzard is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    awesome Dr. Fox thank you so much that worked... now my only problem is when I input hours as 5 for say package a I get a negative value for the charges... I know this has to do with the if else statements however can you please advise me how to input the correct statement should it be something like :

    if (hoursUsed < 10)
    System.out.println(monthlyFee);



    this is the error:

    run:
    Enter the letter of the package you purchased (either A, B, or C.)
    a
    Enter the number of hours you used.
    5
    The total charges is $-0.05000000000000071.
    BUILD SUCCESSFUL (total time: 4 seconds)

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Have a re look at your calculations for package 'A'

    // hoursUsed=5
    // regularHours=10;
    // additionalHours= 5-10 = -5
    // additionalHoursFee = -5*2.00 = -10
    totalFee = 9.95 - 10 = -0.05

    System.out.println(totalFee);

    All your problems begin here when hoursUsed is 5
    Java Code:
    additionalHours = hoursUsed - regularHours;

  8. #8
    twyzard is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    great, everything seems to be ok now. thank you guys so much, I am truly a n00b at Java though I am very interested in it and want to learn it as quickly as possible. Now how do I delete this thread ?

  9. #9
    Rich0818 is offline Member
    Join Date
    Apr 2014
    Posts
    1
    Rep Power
    0

    Default Re: Java code problem with three different internet service provider packages.

    I have this same program assignment except they want me to use set and get methods. So there is a class and a demo program. What do I do?




    Quote Originally Posted by Dr.Fox View Post
    Java Code:
    System.out.println("Enter the number of hours " +
    "you used.");
    hoursUsed = Integer.parseInt(input);
    input = keyboard.nextLine();
    hoursUsed = input.charAt(0);
    Why are you parsing your input before reading the users input of hours? This will make the program try to parse the last input, the letter A, B or C they typed in before.

    Also you dont want to make hoursUsed a char.

    try:
    Java Code:
    System.out.println("Enter the number of hours " +
    "you used.");
    input = keyboard.nextLine();
    hoursUsed = Integer.parseInt(input);

Similar Threads

  1. Java packages / statsic problem - Please help!
    By brendan_1986 in forum New To Java
    Replies: 14
    Last Post: 02-09-2011, 01:51 PM
  2. Replies: 2
    Last Post: 01-28-2011, 06:22 PM
  3. JNDI/DNS Service Provider and Syslog
    By Jotta in forum Networking
    Replies: 0
    Last Post: 04-19-2010, 10:05 PM
  4. Replies: 0
    Last Post: 04-13-2010, 08:16 PM
  5. Help - problem with java when using internet
    By badabing in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •