import java.util.*;
import java.text.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double[] money = {50.00, 20.00, 10.00, 5.00, 1.00, 0.25, 0.10, 0.05, 0.01};
int i = 0;
int j = 0;
double amountDue;
double amountGiven;
double change;
Scanner kbReader = new Scanner(System.in);
System.out.print("Amount Due: ");
amountDue = kbReader.nextDouble();
System.out.print("Amount Given: ");
amountGiven = kbReader.nextDouble();
change = (amountGiven - amountDue);
/*
NumberFormat fmt = NumberFormat.getNumberInstance();
fmt.setMaximumFractionDigits(2);
fmt.setMinimumFractionDigits(2);
String s = fmt.format(change);
*/ // Rounding off change
System.out.println("Change: " + change);
while (change > 0.00) {
while (change >= money[i]) {
change -= money[i];
j++;
}
System.out.println(j + " * " + money[i] + " = " + (money[i] * j));
i++;
j = 0;
}
}
}
2 replies to this topic
#1
Posted 15 March 2011 - 05:33 PM
Hey guys I'm kinda a novice at programming and I was trying to follow the Cash Register project from the event on this site. I followed one of CommittedC0der's posts where he used a while loop to loop through the array of money types to calculate the amount of change given. I pretty much tried to copy his code exactly like he did it, although I was doing it in Java. However, I'm getting a "java.lang.ArrayIndexOutOfBoundsException: 9" error. I know it has something to do with the while loop..I think..but I can't seem to figure it out. Any help would be appreciated.
|
|
|
#2
Posted 15 March 2011 - 06:18 PM
Ahh, welcome to the Floating Point Rounding Demons. Here's your problem, look at what you're doing with the change:
To fix this, you can either try doing this with ints, or you can change your while loop condition:
WARNING: Again, because of floating point, this program may also behave erratically. Remember that when you're performing successive modifications to a float value not to expect it to be precise. Float values are usually used for calculating, for example, the X, Y, and Z locations of an object in 3D space. You should use int's for this, as that will not have any imprecision issues.
while (change > 0.00) {
while (change >= money[i]) {
[b]change -= money[i];[/b]
j++;
}
This means you're subtracting a double from another double, successively. Because of floating point rounding, you won't end up with a perfect fractionally 0 value after the math is done, instead you'll end up with a value like "0.000000000001". This IS greater than 0, which means that the while loop executes another time, and attempts to access the money array with too high a value. This causes an exception to be thrown and your program to quit.To fix this, you can either try doing this with ints, or you can change your while loop condition:
while (change > 0.001)You're not calculating hay pennies, so you'll be fine.
WARNING: Again, because of floating point, this program may also behave erratically. Remember that when you're performing successive modifications to a float value not to expect it to be precise. Float values are usually used for calculating, for example, the X, Y, and Z locations of an object in 3D space. You should use int's for this, as that will not have any imprecision issues.
Wow I changed my sig!
#3
Posted 15 March 2011 - 07:11 PM
Your array only has 8 indices, as the first index is 0. You're getting the error because you're calling the 9th(non-existent) location in the array.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









