Results 1 to 19 of 19
- 05-24-2011, 08:13 PM #1
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
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.
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: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; } } }
Java Code:I want this quantity0 2 This is the total balance0.0 BUILD SUCCESSFUL (total time: 4 seconds)
- 05-24-2011, 08:18 PM #2
Can you show us what you want the output to look like?
If you don't want to see the value of quantityOrdered, why do you have it in the println?Java Code:System.out.println("I want this quantity" +(quantityOrdered));
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.
- 05-25-2011, 03:37 AM #3
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
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.
- 05-25-2011, 03:42 AM #4
If you don't want to see the value of quantityOrdered, why do you have it in the println?
Don't print the contents of quantityOrdered when you don't want to see its value. In this case zero.Please give me some suggestions.
Remove it from the println statement.
- 05-25-2011, 05:00 AM #5
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.Java Code:quantityOrdered = br.read();
Refering to your code in the other thread, you get incorrect results because you perform all the calculations before you get user input.
- 05-25-2011, 05:03 AM #6
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
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.
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:I want this quantity. 2 This is the total balance. 213.3 BUILD SUCCESSFUL (total time: 5 seconds)
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)); } }
- 05-25-2011, 05:05 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
You will always get zero result because you compute paymentBalance and productTotal BEFORE user input.
Java Code:productTotal = priceItem * quantityOrdered; paymentBalance = productTotal * taxPaid;
- 05-25-2011, 05:09 AM #8
When people offer you advice it would be nice if you ACTUALLY READ IT!
- 05-25-2011, 05:10 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
What should be the output?
- 05-25-2011, 05:12 AM #10
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
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
- 05-25-2011, 05:13 AM #11
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
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.
- 05-25-2011, 05:15 AM #12
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!
- 05-25-2011, 05:16 AM #13
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
According to my calculator, I should be getting 8.532 for the payment balance. But I got the 213.3.
- 05-25-2011, 05:17 AM #14
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
Please ignore any of my posts to this forum, Junky. Thank you. God Bless You.
- 05-25-2011, 05:17 AM #15
- 05-25-2011, 05:33 AM #16
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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; }
- 05-25-2011, 05:34 AM #17
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
I do appreciate any helpful input which was given. Thank you.
- 05-25-2011, 05:45 AM #18
That is an incorrect statement. I provided plenty of helpful input way back in reply #5 which apparently wasn't appreciated at all.
- 05-26-2011, 04:00 AM #19
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
Similar Threads
-
Help with user input
By sconniegorilla in forum New To JavaReplies: 2Last Post: 02-16-2011, 02:00 PM -
problem displaying calculation in JText field
By smallmos1 in forum New To JavaReplies: 1Last Post: 02-10-2011, 08:15 PM -
User Input
By brmcdani in forum New To JavaReplies: 2Last Post: 02-05-2010, 01:59 AM -
how to get input from User
By Alvaro in forum New To JavaReplies: 7Last Post: 01-15-2010, 11:02 PM -
need help. problems storing and displaying multiple user input
By dDarko in forum New To JavaReplies: 3Last Post: 10-28-2009, 01:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks