Results 1 to 19 of 19
  1. #1
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default help with displaying calculation with user input

    Hi, I am attempting to have an application which calculates the total and the total with the tax rate. The quantity is input by the user. After the quantity is input by the user the application calculates the total and the total with tax. Please help. Thanks.
    Java Code:
    package products;
    
    /**
     *
     * @author Lekeisha
     */
    
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    
    public class Products {
        public Products()
        {
            
        }
        public static void main(String[] args)
            {        
            Products products1 = new Products();
            products1.inputFromConsole();
            }
        
        public void inputFromConsole()
        {
           
            // TODO code application logic here
            int quantityOrdered = 0;
            double productTotal;
            double priceItem = 3.95;
            double taxPaid = 1.08;
            double paymentBalance;
            
            productTotal = priceItem*quantityOrdered;
            paymentBalance = productTotal*taxPaid;
            
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    
            System.out.println("I want this quantity" +(quantityOrdered));
            
            try
                {    
                    quantityOrdered = br.read();
                }
                catch
                (IOException e)
                {
                    return;
                }    
            
            System.out.println("This is the total balance" +(paymentBalance));
            try
            {
                    paymentBalance = br.read();
                    
            }
            catch
            (IOException e)        
            {
                return;
            }   
                                                         
            }
           }
    This is what I receive when I run it. I do not want the zero to display, just the user input and the results from the calculations. Thanks.
    Java Code:
    I want this quantity0
    2
    This is the total balance0.0
    BUILD SUCCESSFUL (total time: 4 seconds)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Can you show us what you want the output to look like?

    Java Code:
     System.out.println("I want this quantity" +(quantityOrdered));
    If you don't want to see the value of quantityOrdered, why do you have it in the println?

    The output would look a little better if you put a space between the words and the numbers.

    In your catch blocks, add a call to e.printStackTrace() to show a message if there is an error.
    Last edited by Norm; 05-24-2011 at 08:21 PM.

  3. #3
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default What I would like to Display

    I would like it to display whatever the user enters. but the zero pops up in addition to whatever the user types in. Please give me some suggestions. Thanks.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    If you don't want to see the value of quantityOrdered, why do you have it in the println?
    Please give me some suggestions.
    Don't print the contents of quantityOrdered when you don't want to see its value. In this case zero.
    Remove it from the println statement.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    quantityOrdered = br.read();
    This will not do what you want. It will assign the ascii value of the char read to your variable and not the decimal value. Use readLine and convert to interger or use a Scanner instead.

    Refering to your code in the other thread, you get incorrect results because you perform all the calculations before you get user input.

  6. #6
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    I made an adjustment to the I want this quantity line to just get the user input instead of giving me the zero and the user input. Now, I just need to know how to get my calculations to output for the paymentBalance. I want the user to see the results.
    Java Code:
    I want this quantity.
    2
    This is the total balance. 213.3
    BUILD SUCCESSFUL (total time: 5 seconds)
    Those were my results. The price of 3.95 is multiplied by the user input of quantity. This give the productTotal. The product total is multiplied by the taxPaid, 1.08, and this is supposed to give the paymentBalance. According to my calculator, the calculation is wrong. Please help. Thanks.
    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    
            System.out.println("I want this quantity." + "");
            
            try
                {    
                    quantityOrdered = br.read();
                }
                catch
                (IOException e)
                {
                    return;
                }    
            double paymentBalance;
            productTotal = priceItem*quantityOrdered;
            paymentBalance = productTotal*taxPaid;
            System.out.println("This is the total balance. " +(paymentBalance));
            
                    }   
                                                         
            }

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    You will always get zero result because you compute paymentBalance and productTotal BEFORE user input.
    Java Code:
     
            productTotal = priceItem * quantityOrdered;
            paymentBalance = productTotal * taxPaid;

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When people offer you advice it would be nice if you ACTUALLY READ IT!

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What should be the output?

  10. #10
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Okay. You don't have to be mean, Junky. I have read them, but I am a beginner as this forum is titled New to Java. So, please be nice and not rude. #God's Child

  11. #11
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Thanks, mine0926. Thanks for being nice. I appreciate the kindness since I am trying to learn this. It is a little difficult to understand sometimes.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I wasn't mean in my first reply. I explained exactly what was wrong and offered 2 alternative solutions. You chose to ignore my advice and continued to whine about getting incorrect results. I consider it extremely rude when people ignore others!

  13. #13
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    According to my calculator, I should be getting 8.532 for the payment balance. But I got the 213.3.

  14. #14
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Please ignore any of my posts to this forum, Junky. Thank you. God Bless You.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by bamagirl31 View Post
    but I am a beginner as this forum is titled New to Java.
    It has nothing to do with being a beginner and everything to do with common decency. How would you feel if I walked up to you and asked what the time was, then simply turned my back and starting chatting to someone else?

  16. #16
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    As junky suggested, use readLine() instead.
    Because br.readLine() returns String you need to convert it to integer using Integer.parseInt
    Java Code:
            try
            {
                quantityOrdered = Integer.parseInt(br.readLine());
            }
            catch
            (IOException e)
            {
                return;
            }

  17. #17
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    I do appreciate any helpful input which was given. Thank you.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    That is an incorrect statement. I provided plenty of helpful input way back in reply #5 which apparently wasn't appreciated at all.

  19. #19
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default Thanking everybody

    That last post was for everybody including you, Junky. I appreciate the help. Thanks. God Bless.

Similar Threads

  1. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  2. problem displaying calculation in JText field
    By smallmos1 in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 08:15 PM
  3. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  4. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  5. Replies: 3
    Last Post: 10-28-2009, 01:45 AM

Posting Permissions

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