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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: JAVA help on file: Internet Service Provider

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

    Default JAVA help on file: Internet Service Provider

    What to do with this JAVA code?
    This is the code that I need to do:
    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.

    I keep getting either negative prices or a price that is less than the minimum access fee (when I input e.g. A and 4 --> answer will be $-2.0500000000000007 instead of $9.95).
    I want the answer to be either $9.95 or $13.95 when I enter the number of hours less than the number of hours allowed to access the internet.
    What am I missing in this code?
    Also, what can I do to make it into 2 decimal places instead of 16 decimal places (without using printf or decimalFormatter)?

    Here is my code source:
    package internetserviceprovider;
    import javax.swing.JOptionPane;
    /**
    *
    * @author Home
    */
    public class Main {
     
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    char packageLetter;
    int hoursUsed;
    int regularHours;
    int additionalHours;
    double monthlyFee;
    double additionalHoursFee;
    double totalFee;
    String inputString;
     
    inputString = JOptionPane.showInputDialog("Enter the letter of the " +
    "package you purchased (either A, B, or C.");
    inputString = inputString.toUpperCase();
    packageLetter = inputString.charAt(0);
     
    inputString = JOptionPane.showInputDialog("Enter the number of hours " +
    "you used.");
    hoursUsed = Integer.parseInt(inputString);
     
    switch(packageLetter)
    {
    case 'A':
    monthlyFee = 9.95;
    regularHours = 10;
    additionalHours = hoursUsed - regularHours;
    additionalHoursFee = additionalHours * 2.00;
    totalFee = monthlyFee + additionalHoursFee;
    JOptionPane.showMessageDialog(null,"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);
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    case 'C':
    totalFee = 19.95;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    default:
    JOptionPane.showMessageDialog(null,"Plea… enter either A,B, " +
    "or C).");
    }
    System.exit(0);
     
    }
     
    }
    Last edited by Plural; October 7th, 2010 at 07:13 PM.


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

    Default Re: JAVA help on file: Internet Service Provider

    Do you have any specific java programming questions?

    I keep getting either negative prices or a price that is less than the minimum access fee.
    Try debugging your code by printing out the values of all the variables used as your code computes

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    Do you have any specific java programming questions?


    Try debugging your code by printing out the values of all the variables used as your code computes
    What I meant was that I am able to get the value for the computations but not the minimum value:
    for example, for case 'A' I enter 4 hours and I will get $-2.050000000000007 instead of $9.95.
    In addition, how do I make it into 2 decimal places without using printf and decimal formatter?

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

    Default Re: JAVA help on file: Internet Service Provider

    how do I make it into 2 decimal places without using printf and decimal formatter
    Are you talking about when you format a double value to be printed?
    One way to truncate a double to 2 places is multiply it by 100, cast it to an int and then divide it by 100 to get back the original significance.

    not the minimum value:
    for example, for case 'A' I enter 4 hours and I will get $-2.050000000000007 instead of $9.95.
    Have you tried debugging your code by print out the values as they are computed to show you where the mistake is?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    Are you talking about when you format a double value to be printed?
    One way to truncate a double to 2 places is multiply it by 100, cast it to an int and then divide it by 100 to get back the original significance.


    Have you tried debugging your code by print out the values as they are computed to show you where the mistake is?
    To the first question:
    I want to change the answer that would show in the message dialog as a 2 decimal value instead of the 16 decimal value.

    To the second question:
    Yes, I've debugged it. It has no mistakes.

    The answer is supposed to be either
    package A --> $9.95 for 10 hours or less; normal computation occurs
    package B --> $13.95 for 20 hours or less; normal computation occurs
    package C --> $19.95 for unlimited access

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

    Default Re: JAVA help on file: Internet Service Provider

    Yes, I've debugged it. It has no mistakes.
    Does that mean that the program is working?

    What do you mean by mistake? What do you call this:
    for case 'A' I enter 4 hours and I will get $-2.050000000000007 instead of $9.95.

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Yes, the program works.
    As you can see, the program is suppose to compute the fee for the current month's internet service.

    For the first question from my code:
    It asks me to input the letter of the package.
    e.g. I would enter the letter 'A'.

    For the second question from my code:
    It asks me to enter the amount of hours that I've accessed the internet for.
    e.g. I would enter 15.

    The answer would be $19.95.



    On contrary, here is what I'm stuck on my code.
    For the first question:
    e.g. I would enter the letter 'A'.

    For the second question:
    e.g. I would enter 1.

    The answer would be $-8.05.
    It should've been $9.95 because it is less than 10 hours of access.

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

    Default Re: JAVA help on file: Internet Service Provider

    Is the program making a mistake then?
    There are 5 lines of code in the area in question. Take a piece of paper and a pencil and play computer executing each statement in your mind and writing down the results for all 5 lines. See what answer you get. Is it correct?
    If not what do you need to consider to correct it? hint: you may need an if

  9. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    Does that mean that the program is working?

    What do you mean by mistake? What do you call this:
    Quote Originally Posted by Norm View Post
    Is the program making a mistake then?
    There are 5 lines of code in the area in question. Take a piece of paper and a pencil and play computer executing each statement in your mind and writing down the results for all 5 lines. See what answer you get. Is it correct?
    If not what do you need to consider to correct it? hint: you may need an if
    All the answers that is above 10 hours for 'A' and 20 hours for 'B' are correct expect the ones that are under 10 hours for 'A' and under 20 hours for 'B' are incorrect.
    For 'A':
    I can enter any number from 1-10, it should be equal to $9.95 without any additional charges.
    Whereas, any number above 10 hours starting from 11 should be $9.95 + ($2.00 per additional hour).
    For 'B'
    I can enter any number from 1-20, it should be equal to $13.95 without any additional charges.
    Whereas, any number above 20 hours starting from 21 should be $13.95 + ($1.00 per additional hour).

    I was thinking of putting an "if statement" but not sure where to insert it.
    I just started using JAVA so I'm not sure if I can insert an "if statement" inside a "switch statement".

  10. #10
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: JAVA help on file: Internet Service Provider

    I was thinking about it, and there is an unnecessary step in your java code:
    int regularHours;
    ..
    regularHours = 10;
    ..
    regularHours = 20;

    Try it without that added variable

    package internetserviceprovider;
    import javax.swing.JOptionPane;
    /**
    *
    * @author Home
    */
    public class Main {
     
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    char packageLetter;
    int hoursUsed;
    int additionalHours;
    double monthlyFee;
    double additionalHoursFee;
    double totalFee;
    String inputString;
     
    inputString = JOptionPane.showInputDialog("Enter the letter of the " +
    "package you purchased (either A, B, or C.");
    inputString = inputString.toUpperCase();
    packageLetter = inputString.charAt(0);
     
    inputString = JOptionPane.showInputDialog("Enter the number of hours " +
    "you used.");
    hoursUsed = Integer.parseInt(inputString);
     
    switch(packageLetter)
    {
    case 'A':
    monthlyFee = 9.95;
    additionalHours = hoursUsed - 10;
    additionalHoursFee = additionalHours * 2.00;
    totalFee = monthlyFee + additionalHoursFee;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    case 'B':
    monthlyFee = 13.95;
    additionalHours = hoursUsed - 20;
    additionalHoursFee = additionalHours * 1.00;
    totalFee = monthlyFee + additionalHoursFee;
    System.out.println("The total charges is " + totalFee);
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    case 'C':
    totalFee = 19.95;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    default:
    JOptionPane.showMessageDialog(null,"Plea… enter either A,B, " +
    "or C).");
    }
    System.exit(0);
     
    }
     
    }

    Also
    JOptionPane.showMessageDialog(null,"Plea… enter either A,B, " +
    "or C).");
    has a ) at the end inside the "", but without one in the beginning, looks a little weird
    Last edited by Tjstretch; October 7th, 2010 at 11:11 PM.

  11. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Tjstretch View Post
    I was thinking about it, and there is an unnecessary step in your java code:
    int regularHours;
    ..
    regularHours = 10;
    ..
    regularHours = 20;

    Try it without that added variable

    package internetserviceprovider;
    import javax.swing.JOptionPane;
    /**
    *
    * @author Home
    */
    public class Main {
     
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    char packageLetter;
    int hoursUsed;
    int additionalHours;
    double monthlyFee;
    double additionalHoursFee;
    double totalFee;
    String inputString;
     
    inputString = JOptionPane.showInputDialog("Enter the letter of the " +
    "package you purchased (either A, B, or C.");
    inputString = inputString.toUpperCase();
    packageLetter = inputString.charAt(0);
     
    inputString = JOptionPane.showInputDialog("Enter the number of hours " +
    "you used.");
    hoursUsed = Integer.parseInt(inputString);
     
    switch(packageLetter)
    {
    case 'A':
    monthlyFee = 9.95;
    additionalHours = hoursUsed - 10;
    additionalHoursFee = additionalHours * 2.00;
    totalFee = monthlyFee + additionalHoursFee;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    case 'B':
    monthlyFee = 13.95;
    additionalHours = hoursUsed - 20;
    additionalHoursFee = additionalHours * 1.00;
    totalFee = monthlyFee + additionalHoursFee;
    System.out.println("The total charges is " + totalFee);
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    case 'C':
    totalFee = 19.95;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    default:
    JOptionPane.showMessageDialog(null,"Plea… enter either A,B, " +
    "or C).");
    }
    System.exit(0);
     
    }
     
    }

    Also
    JOptionPane.showMessageDialog(null,"Plea… enter either A,B, " +
    "or C).");
    has a ) at the end inside the "", but without one in the beginning, looks a little weird
    I've done the things that you've asked me to change.
    The problem is pretty similar to my question.
    How do I make the minimum monthly fee appear instead of it being less?
    e.g. package 'A': 10 hours or less of access should be equal to $9.95, no lower than that.
    e.g. package 'B': 20 hours or less of access should be equal to $13.95, no lower than that.
    Computation should occur any amount of hours higher than that and charged with an additional fee.

  12. #12
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: JAVA help on file: Internet Service Provider

    So it did not fix the problem? If not than look at this:

    It is always doing the statement
    additionalHoursFee = additionalHours * 2.00;
    additionalHours is negative if it is below 10 (in case A) because of
    additionalHours = hoursUsed - 10;
    so consider
    if(additionalHours>0){
    additionalHoursFee = additionalHours * 2.00;
    }

    So in case A it would look like
    case 'A':
    monthlyFee = 9.95;
    additionalHours = hoursUsed - 10;
    if(additionalHours>0)
    {
      additionalHoursFee = additionalHours * 2.00;
    }
    totalFee = monthlyFee + additionalHoursFee;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;

    Do that for every case, that will almost definitely fix the problem.

    Oh it will give you an error, to solve that in your initial int statements make the additionalHoursFee look like
    int additionalHoursFee = 0;

  13. The Following User Says Thank You to Tjstretch For This Useful Post:

    JavaPF (October 8th, 2010)

  14. #13
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Tjstretch View Post
    I was thinking about it, and there is an unnecessary step in your java code:
    int regularHours;
    ..
    regularHours = 10;
    ..
    regularHours = 20;

    Try it without that added variable

    package internetserviceprovider;
    import javax.swing.JOptionPane;
    /**
    *
    * @author Home
    */
    public class Main {
     
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    char packageLetter;
    int hoursUsed;
    int additionalHours;
    double monthlyFee;
    double additionalHoursFee;
    double totalFee;
    String inputString;
     
    inputString = JOptionPane.showInputDialog("Enter the letter of the " +
    "package you purchased (either A, B, or C.");
    inputString = inputString.toUpperCase();
    packageLetter = inputString.charAt(0);
     
    inputString = JOptionPane.showInputDialog("Enter the number of hours " +
    "you used.");
    hoursUsed = Integer.parseInt(inputString);
     
    switch(packageLetter)
    {
    case 'A':
    monthlyFee = 9.95;
    additionalHours = hoursUsed - 10;
    additionalHoursFee = additionalHours * 2.00;
    totalFee = monthlyFee + additionalHoursFee;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    case 'B':
    monthlyFee = 13.95;
    additionalHours = hoursUsed - 20;
    additionalHoursFee = additionalHours * 1.00;
    totalFee = monthlyFee + additionalHoursFee;
    System.out.println("The total charges is " + totalFee);
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    case 'C':
    totalFee = 19.95;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;
    default:
    JOptionPane.showMessageDialog(null,"Plea… enter either A,B, " +
    "or C).");
    }
    System.exit(0);
     
    }
     
    }

    Also
    JOptionPane.showMessageDialog(null,"Plea… enter either A,B, " +
    "or C).");
    has a ) at the end inside the "", but without one in the beginning, looks a little weird
    Quote Originally Posted by Tjstretch View Post
    So it did not fix the problem? If not than look at this:

    It is always doing the statement
    additionalHoursFee = additionalHours * 2.00;
    additionalHours is negative if it is below 10 (in case A) because of
    additionalHours = hoursUsed - 10;
    so consider
    if(additionalHours>0){
    additionalHoursFee = additionalHours * 2.00;
    }

    So in case A it would look like
    case 'A':
    monthlyFee = 9.95;
    additionalHours = hoursUsed - 10;
    if(additionalHours>0)
    {
      additionalHoursFee = additionalHours * 2.00;
    }
    totalFee = monthlyFee + additionalHoursFee;
    JOptionPane.showMessageDialog(null,"The total charges is $" +
    totalFee + ".");
    break;

    Do that for every case, that will almost definitely fix the problem.

    Oh it will give you an error, to solve that in your initial int statements make the additionalHoursFee look like
    int additionalHoursFee = 0;
    The brackets for the "if statement" makes the variable "additionalHoursFee" not initialized for the statement "totalFee = monthlyFee + additionalHoursFee;"
    Even when I tried it with another way, it still goes negative for some values that I input.

  15. #14
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: JAVA help on file: Internet Service Provider

    You didn't read the end did you:

    Do what I said in my last post and add at the very top instead of
     double additionalHoursFee;
    do
     double additionalHoursFee = 0;

    That initializes the statement.

    Well I accidentally put int in my last post, fail.

  16. #15
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    It works but now I want to change a bit instead of using the switch statement:

    I'm not too familiar with using if-else if-else statements.

    Here's the code source:
    package javaapplication11;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double monthlyFee = 0;
            double additionalHoursFee;
            double totalFee;
            String inputString;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            {
            if(packageLetter=='A')
                monthlyFee = 9.95;
                additionalHours = hoursUsed - 10;
                {
                    if(additionalHours > 0 && hoursUsed > 10)
                    {
                    additionalHoursFee = additionalHours * 2.00;
                    totalFee = monthlyFee + additionalHoursFee;
                    JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
                    }
                else
                {
                        JOptionPane.showMessageDialog(null,"The total charges is $"
                                + monthlyFee + ".");
                    }
     
            if (packageLetter == 'B')
                monthlyFee = 13.95;
                additionalHours = hoursUsed - 20;
                if (additionalHours>0 && hoursUsed>20)
                    {
                    additionalHoursFee = additionalHours * 1.00;
                    totalFee = monthlyFee + additionalHoursFee;
                    JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
                    }
                else
                    {
                        JOptionPane.showMessageDialog(null,"The total charges is $"
                                + monthlyFee + ".");
                    }
     
                if(packageLetter == 'C')
                {
                    totalFee = 19.95;
                    JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
                }
     
                if(packageLetter !='A'||packageLetter !='B'||packageLetter !='C')
                {
                    JOptionPane.showMessageDialog(null,"Please enter either A,B, " +
                            "or C).");
                }
            }
     
            System.exit(0);
        }
    }

    Many message dialogs pop up for the answer. How am I missing to make it just one message dialog to pop up?

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

    Default Re: JAVA help on file: Internet Service Provider

    Many message dialogs pop up for the answer.
    Can you explain which messages pop up?
    You need to follow the logic of your program and see why each message is shown. What controls why each message is shown?

    Your usage of { & } after an if needs to be checked. There are many that are in the wrong places.
    Without the {} only the immediately following statement is controlled by the if.
    Last edited by Norm; October 8th, 2010 at 07:13 AM.

  18. #17
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    Can you explain which messages pop up?
    You need to follow the logic of your program and see why each message is shown. What controls why each message is shown?

    Your usage of { & } after an if needs to be checked. There are many that are in the wrong places.
    Without the {} only the immediately following statement is controlled by the if.
    After execution, the message dialog appears for:
    package A
    package B
    the default

    So...therefore, I have to use else if? When I did that the error on side says that it should be "else without if".
    How do I make that separate each just like the "switch statement"?

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

    Default Re: JAVA help on file: Internet Service Provider

    Did you check and correct the problems with the { & }s?

  20. #19
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    Can you explain which messages pop up?
    You need to follow the logic of your program and see why each message is shown. What controls why each message is shown?

    Your usage of { & } after an if needs to be checked. There are many that are in the wrong places.
    Without the {} only the immediately following statement is controlled by the if.
    Quote Originally Posted by Norm View Post
    Did you check and correct the problems with the { & }s?
    I've changed it but on the sides, it says identifier expected and I'm not too sure where to put the brackets for the "if-else if-else".

    package javaapplication11;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double monthlyFee = 0;
            double additionalHoursFee;
            double totalFee;
            String inputString;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            {
            if(packageLetter=='A'&& additionalHours > 0 && hoursUsed > 10)
                monthlyFee = 9.95;
                additionalHours = hoursUsed - 10;
                    additionalHoursFee = additionalHours * 2.00;
                    totalFee = monthlyFee + additionalHoursFee;
                    JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
     
                else if(packageLetter=='B' && additionalHours>0 && hoursUsed>20)
                monthlyFee = 13.95;
                additionalHours = hoursUsed - 20;
                    additionalHoursFee = additionalHours * 1.00;
                    totalFee = monthlyFee + additionalHoursFee;
                    JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
     
                else if(packageLetter=='C')
                    totalFee = 19.95;
                    JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".")
     
                else
                    JOptionPane.showMessageDialog(null,"Please enter either A,B, " +
                            "or C).");
            }
     
            System.exit(0);
     
        }    
    }

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

    Default Re: JAVA help on file: Internet Service Provider

    ALWAYS put a { after the ending ) on a if statement:
    if (some conditions here) {

    then put the end } after the last statement(s) do be done when the if condition is true. Put it on its own line.


    Your posted code doesn't have any { that I can see.
    You need to add them as I stated above.
    Last edited by Norm; October 8th, 2010 at 09:23 AM.

  22. #21
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    ALWAYS put a { after the ending ) on a if statement:
    if (some conditions here) {

    then put the end } after the last statement(s) do be done when the if condition is true. Put it on its own line.


    Your posted code doesn't have any { that I can see.
    You need to add them as I stated above.
    I've fixed those.
    Now, whatever I enter...it becomes "Please enter either A, B, or C."

    package javaapplication11;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double monthlyFee = 0;
            double additionalHoursFee;
            double totalFee;
            String inputString;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            {
            if(packageLetter=='A'&& additionalHours > 0 && hoursUsed > 10)
            {
                monthlyFee = 9.95;
                additionalHours = hoursUsed - 10;
                additionalHoursFee = additionalHours * 2.00;
                totalFee = monthlyFee + additionalHoursFee;
                JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
            }
            else if(packageLetter=='B' && additionalHours>0 && hoursUsed>20)
            {
                monthlyFee = 13.95;
                additionalHours = hoursUsed - 20;
                additionalHoursFee = additionalHours * 1.00;
                totalFee = monthlyFee + additionalHoursFee;
                JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
            }
            else if(packageLetter=='C')
            {
                totalFee = 19.95;
                JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
            }
     
                else
                    JOptionPane.showMessageDialog(null,"Please enter either A,B, " +
                            "or C).");
            }
     
            System.exit(0);
     
        }
    }

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

    Default Re: JAVA help on file: Internet Service Provider

    whatever I enter...it becomes "Please enter either A, B, or C."
    Enter what?
    What is the "it" that becomes?

    I don't understand what you are trying to explain. Can you say what you do and what the computer does, step by step?
    if(packageLetter !='A'||packageLetter !='B'||packageLetter !='C')
    What if packageLetter is 'C'?
    Is that not equal to 'A'
    so packageLetter !='A' would be true and the if true statement would execute
    You want all three conditions to be true not just one of them. Use AND vs OR logic here

  24. #23
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    Enter what?
    What is the "it" that becomes?

    I don't understand what you are trying to explain. Can you say what you do and what the computer does, step by step?
    if(packageLetter !='A'||packageLetter !='B'||packageLetter !='C')
    What if packageLetter is 'C'?
    Is that not equal to 'A'
    so packageLetter !='A' would be true and the if true statement would execute
    You want all three conditions to be true not just one of them. Use AND vs OR logic here

    if(packageLetter!='A' && packageLetter!='B' && packageLetter!='C')
            {
                JOptionPane.showMessageDialog(null, "Please enter either A,B, "
                        + "or C).");
            }

    After changing that part of the code, nothing pops up when I enter 'A' or 'B' along with the hours.
    Only 'C' along with hours, a message dialog pops up saying "The total charges is $19.95."
    This part of the code is supposed to have the result compared to the "switch statement's" default function.

    For the previous question:
    I entered 'A' and any number of hours --> "Please enter either A, B, or C."
    I entered 'B' and any number of hours --> "Please enter either A, B, or C."
    I entered 'C' and any number of hours --> "The total charges is $19.95."

    A new error has also occured at the end of the entire code saying "reached end of file while parsing."

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

    Default Re: JAVA help on file: Internet Service Provider

    "reached end of file while parsing."
    Check for matching pairs of {}s
    Make sure they are all paired.

  26. #25
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA help on file: Internet Service Provider

    Quote Originally Posted by Norm View Post
    "reached end of file while parsing."
    Check for matching pairs of {}s
    Make sure they are all paired.
    The brackets seem to pair up nicely...
    The last bracket, isn't it supposed to pair up with the one after "public static void"?

    package javaapplication11;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double monthlyFee = 0;
            double additionalHoursFee;
            double totalFee;
            String inputString;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
        {
            if(packageLetter=='A'&& additionalHours > 0 && hoursUsed > 10)
            {
                monthlyFee = 9.95;
                additionalHours = hoursUsed - 10;
                additionalHoursFee = additionalHours * 2.00;
                totalFee = monthlyFee + additionalHoursFee;
                JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
            }
            else if(packageLetter=='B' && additionalHours>0 && hoursUsed>20)
            {
                monthlyFee = 13.95;
                additionalHours = hoursUsed - 20;
                additionalHoursFee = additionalHours * 1.00;
                totalFee = monthlyFee + additionalHoursFee;
                JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
            }
            else if(packageLetter=='C')
            {
                totalFee = 19.95;
                JOptionPane.showMessageDialog(null,"The total charges is $" +
                            totalFee + ".");
            }
            if(packageLetter!='A' && packageLetter!='B' && packageLetter!='C')
            {
                JOptionPane.showMessageDialog(null, "Please enter either A,B, "
                        + "or C).");
            }
     
            System.exit(0);
     
        }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Java web client - > .NET web service
    By codeJ in forum Web Frameworks
    Replies: 0
    Last Post: July 8th, 2010, 04:34 AM
  2. Replies: 2
    Last Post: November 19th, 2009, 11:55 PM
  3. Read XML from Internet Hang
    By yupingliew in forum Java Networking
    Replies: 8
    Last Post: November 9th, 2009, 09:49 AM
  4. Free java hosting service providers
    By servlet in forum Java Servlet
    Replies: 1
    Last Post: May 14th, 2009, 07:28 AM
  5. Internet Filter to display some website
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 15th, 2008, 05:03 AM