• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output not resulting in double?  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output for some reason is not resulting in a double? If i remove the int it gives an error.


 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why it's necessary to use double variables for the loop indexes. Just use int variables; like this:


 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it gives an error that says add cast to int. the problem that im having is that output does not show decimals. for an example look at the last set of numbers why is it only $2.0? why not not the decimal??


ANSWERS
Enter the price for each item: 1.12
Enter the price for each item: 2.36
Enter the price for each item: 3.25
Enter the price for each item: 4.25
Enter the price for each item: 7.36
Enter the price for each item: 9.99
Enter the price for each item: 8.49
Enter the price for each item: 1.35
Enter the price for each item: 1.26
Enter the price for each item: 3.45

Enter the amount for each item: 2
Enter the amount for each item: 3
Enter the amount for each item: 4
Enter the amount for each item: 1
Enter the amount for each item: 5
Enter the amount for each item: 6
Enter the amount for each item: 8
Enter the amount for each item: 3
Enter the amount for each item: 4
Enter the amount for each item: 5

The total for each item is: $2.0
The total for each item is: $6.0
The total for each item is: $12.0
The total for each item is: $4.0
The total for each item is: $35.0
The total for each item is: $54.0
The total for each item is: $64.0
The total for each item is: $3.0
The total for each item is: $4.0
The total for each item is: $15.0
*/


 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because something tells you to "cast to int" doesn't mean that you have to cast every single double value in your entire program to an int.

In particular at line 32 of your posted code you're taking the price you input (e.g. $1.12) and casting it to an int (e.g. $1), which is why you're getting surprising output.

I'd suggesting looking at the "cast to int" advice and apply it only to the specific value it refers to.

To start with don't use double variables for your loop indexes. As for the data you enter: Price should be double (so you can use $1.12) but amount should be int (you can only buy an integer number of whatsits).

If you're still stuck, post details of your problem.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is lots more to say:-
  • 1: Why are you using a BufferedReader for keyboard input when a Scanner will make it easier? Why are you creating a new reader every time the loop runs?
  • 2: Why have you got everything in the one method? You sh‍ould have multiple methods, one for input, one for calculations, one for display, etc.
  • That looks a very non‑object‑oriented style; I think you sh‍ould have a Purchase class and a Purchase[] array, and each Purchase instance can calculate the total cost.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!