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

Thread: ISP Billing code issues for a beginner

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ISP Billing code issues for a beginner

    Before I post this I just want to make clear that I just started coding like last week so i may seem a little slow.

    Im currently having issues with an assignment regarding Internet Service provider billing. Ive tried several different sources but cannot seem to get an answer as to why im getting a initialization error. Im very very new to this stuff and its probably something basic that i just cant see. The assignment reads:

    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.



    The code i have so far is :
    import java.util.Scanner;
    public class InternetBill
    {
    public static void main(String[] args)
    {
     
    //naming variables 
    Scanner keyboard = new Scanner(System.in);
    int hoursUsed,regularHours,extraHours;
    double monthlyFee,extraHoursFee,totalFee;
    char packageLetter;
     
     
    System.out.println("Enter the type of internet you want(A,B,C)");
     
     
     
    System.out.println("Enter the number of hours you used");
    hoursUsed = keyboard.nextInt();
     
     
     
     
    switch(packageLetter)
     
    //the calculations needed for result 
    {
    /////////////////////////////////////////////////////////////
    case 'A':
    monthlyFee = 9.95;
    regularHours = 10;
    extraHours = hoursUsed - regularHours;
    extraHoursFee = extraHours * 2.00;
    totalFee = monthlyFee + extraHoursFee;
    System.out.println("The total bill is $" + totalFee);
    break;
    /////////////////////////////////////////////////////////////
    case 'B':
    monthlyFee = 13.95;
    regularHours = 20;
    extraHours = hoursUsed - regularHours;
    extraHoursFee = extraHours * 1.00;
    totalFee = monthlyFee + extraHoursFee;
    System.out.println("The total bill is " + totalFee);
    System.out.println("The total bill is $" +totalFee);
    break;
    /////////////////////////////////////////////////////////////
    case 'C':
    totalFee = 19.95;
    System.out.println("The total bill is $" +totalFee + ".");
     
    break;
    default:
    System.out.println("Eenter  A,B,C)");
    }
     
     
    }
     
    }


    The problem I have is that I do not know how to code right under


    System.out.println("Enter the type of internet you want(A,B,C)");

    I want to make it so that the user could enter either A B or C. I tired using "packageLetter=keyboard.nextChar();" but apparently you cant do that. Im so confused -_-.

    I KNOW my code is good with the calculation part and the switch part but i dont know what to do from here


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ISP Billing code issues for a beginner

    im getting a initialization error
    Can you copy the full text of the error message and paste it here?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ISP Billing code issues for a beginner

    sure, it says

    [java code]

    ----jGRASP exec: javac -g InternetBill.java

    InternetBill.java:25: error: variable packageLetter might not have been initialized
    switch(packageLetter)
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    [/code]

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: ISP Billing code issues for a beginner

    Hi. Check out Java For Complete Beginners - user input on how to use Scanner.

    Scanner does not have a nextChar() method. See java - Why doesn't the Scanner class have a nextChar method? - Stack Overflow for the reason. See also java - Take a char input from the Scanner - Stack Overflow on what you can do about it.

    Hth!

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: ISP Billing code issues for a beginner

    Hi try to study this:
    Scanner (Java Platform SE 7 )

    There are many ways to get an input from the users.
    That scanner is one way of getting input

    You can also try study InputStreamReader and BufferedReader. That's an alternative

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ISP Billing code issues for a beginner

    What type of scanner should i use? And whats the code for it?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ISP Billing code issues for a beginner

    variable packageLetter might not have been initialized
    does the code assign a value to variable packageLetter before it is used?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: ISP Billing code issues for a beginner

    What type of scanner should i use? And whats the code for it?
    Try to read the links that we have posted.
    There is a tutorials in that link.
    try to read the first link that jashburn posted,
    there is a tutorial in that link,
    you can also use the link that I have poset for reference

  9. #9
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: ISP Billing code issues for a beginner

    Further guidance... "InternetBill.java:25: error: variable packageLetter might not have been initialized" states that "packageLetter" hasn't been initialised, which is the missing bit beneath

    System.out.println("Enter the type of internet you want(A,B,C)");

    One of the links I posted provides the code to get a char out of the String that is returned by Scanner. The above error message should go away once you assign the char value to packageLetter.

    Ad dicdic mentioned, just go through the links, and you'll be fine.

Similar Threads

  1. Split String and for loop issues. (Beginner problems)
    By AT-LOW in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 03:02 PM
  2. phone billing system task help
    By robbo in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2012, 07:10 AM
  3. (Beginner/Intermediate) Tic Tac Toe game issues
    By Sylentwolf8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 17th, 2012, 08:03 AM
  4. URGENT Need help with code for billing
    By ab3lr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 13th, 2011, 11:44 AM
  5. cable company billing..need help with methods
    By printmatic in forum What's Wrong With My Code?
    Replies: 12
    Last Post: November 7th, 2010, 04:48 PM

Tags for this Thread