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

Thread: My output is in the wrong order! (if-else) PLEASE HELP

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

    Default My output is in the wrong order! (if-else) PLEASE HELP

    This is the task:
    Task #1 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 > 30*24) is input, the program should display an appropriate error message and stop.

    So the input must be either 'A', 'B', or 'C' and anything else should send a message saying "invalid input" and not ask me for more info.

    i should be getting:
    Enter the customer's package (A, B, or C): a
    Invalid package. Enter A, B, or C.

    but i get:
    Enter the customer's package (A, B, or C): a
    Enter the number of hours used:
    Invalid package. Enter A, B, or C.

    if the input is invalid, i dont want it to ask me for hours.
    PLEASE HELP!


    heres the code:

    import java.util.Scanner;

    public class TestInternetPart1
    {
    public static void main(String[] args)
    {
    int hours=1;
    double totalCharges;
    int additionalHours;

    // Create a Scanner object to read input
    Scanner keyboard = new Scanner(System.in);

    // Get the letter of the package
    System.out.print("Enter the customer's package (A, B, or C): ");
    String inputLetter = keyboard.nextLine();
    char internetPackage = inputLetter.charAt(0);

    // Get number of hours.
    System.out.print("Enter number of hours: ");
    hours = keyboard.nextInt();

    if (internetPackage == 'A' && hours > 10)
    {
    additionalHours = hours - 10;
    totalCharges = 9.95 + (additionalHours * 2.00);
    System.out.println("Your monthly bill is $" + totalCharges + ".");
    }
    else if (internetPackage == 'A' && hours < 10)
    {
    System.out.println("Your monthly bill is $9.95.");
    }
    else if (internetPackage == 'B' && hours > 20)
    {
    additionalHours = hours - 20;
    totalCharges = 13.95 + (additionalHours * 1.00);
    System.out.println("Your monthly bill is $" + totalCharges + ".");
    }
    else if (internetPackage == 'B' && hours < 20)
    {
    System.out.println("Your monthly bill is $13.95.");
    }
    else if (internetPackage == 'C')
    {
    System.out.println("Your monthly bill is $19.95.");
    }
    else
    System.out.println("Invalid input. Please enter either A, B, or C.");

    System.exit(0);
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My output is in the wrong order! (if-else) PLEASE HELP

    When posting code, please use the highlight tags to preserve formatting. Unformatted code is pretty impossible to read.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: My output is in the wrong order! (if-else) PLEASE HELP

    After a quick look, my suggestion for you is to go through your code and write out what each line does, kind of like what you did with your comments. You declare you variables, prompt input for package, prompt input for hours, THEN "if" statements that are executed based upon the values of your input.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Location
    Nigeria, Lagos
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Re: My output is in the wrong order! (if-else) PLEASE HELP

    why not first check if internPackage is A or B or C like:
    if(internetPackage == 'A'){
     //todo code here
    } else{
      //todo code here
    }
    After which you can start checking for plan. Although this can make your code a bit much but you can also use switch case (if you're using jdk7);

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: My output is in the wrong order! (if-else) PLEASE HELP

    adeloyedeji, please be mindful of post dates. This post is nearing a year old

Similar Threads

  1. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  2. Sudoku: wrong output?
    By lisa92 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 15th, 2012, 12:10 PM
  3. Replies: 3
    Last Post: October 19th, 2011, 11:55 PM
  4. Replies: 3
    Last Post: February 22nd, 2011, 08:43 PM
  5. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM