# Double Array problem

• 05-02-2011, 07:38 AM
Double Array problem
Hello everyone,
I was doing some readings and searched through the Internet for hours, but I haven't found anything to fix my problem. The goal of my program is simple - the user enteres money and price, and the program calculates the amount of change that is needed to be given, going through the array untill the amount that needs to be given is 0, finds the smallest or equal amount from the array, it gets deducted and so on till there's none required to give. I don't want to make anything complicated.
I looked at BigDecimal - doesn't help me! And the examples I've seen are too complicated as well! And I've tried many different things, but nothing works.
I was also only being unable to register on this forum from another computer, because for some reason it wouldn't let me left-click on tick boxes and some text boxes on the site. So here is the program from my memory:
Code:

``` ...  static void calcChange(double money, double price) {     double cost = money - price;     System.out.println(cost);     double[] avalChange = {50.00, 20.00, 10.00, 5.00, 2.00, 0.50, 0.10};     if (cost > 0) {         while (double cost != 0) {                 for(int i = 0, i <= avalChange.length, i++)         {                 if (avalChange[i] <= change)                 {                         change = avalChange[i];                         cost = cost - change;                         System.out.println("now the cost is " + cost);                 }         }                 }       } }```
Its giving me an error about being out of bound being like that. I've also tried making the last bit this way:
for(double lookChange : avalChange)
{
if (change >= avalChange[i])
{
change = avalChange[i];
cost = cost - change;
System.out.println("now the cost is " + cost);
}
}
And it didn't like that Im trying to use double to look at integer index. Which I understand is what its trying to tell me. But I have completely no idea, of how to change it so it works!
• 05-02-2011, 02:44 PM
ojn
The length of avalChange is 7.
Code:

`for(int i = 0, i <= avalChange.length, i++)`
The last time it steps through this it tries to access avalChange[7] (as avalChange.length will be 7), which would need it to be 8 long. As it's not.

Also, don't paraphrase error messages, copy and paste the exact message the compiler gives you. That makes it so much easier to help. I'm pretty sure it didn't say being out of bound being like that
• 05-02-2011, 11:32 PM
If I do it this way:
Code:

```              for (int i=0; i <= avalChange.length; i++) {                     System.out.println(i);                     if (cost >= avalChange[i])                     {                         change = cost;                         cost = cost - change;                         System.out.println("change is : " + change);                         System.out.println("the cost now is: " + cost);                     }                 }```
It says:

Quote:

java.lang.ArrayIndexOutOfBoundsException:
7
If I do it this way:

Code:

```              for (double changeVal : avalChange) {                 //for (int i=0; i <= avalChange.length; i++) {                     System.out.println(changeVal);                     if (cost >= avalChange[changeVal])                     {                         change = cost;                         cost = cost - change;                         System.out.println("change is : " + change);                         System.out.println("the cost now is: " + cost);                     }                 }```
It says:

Quote:

possible loss of precision:
found :double required: int
I KNOW why its giving me those errors! But I have no idea, of how to make it go through the array, find the value within it, compare it to the cost, and if there's one that matched best, then it shall be deducted from it. I just dont know of how to refer to index and value of the array, say avalChange[i, changeVal]. So that if the cost is 10.00, then the loop will find avalChange[3, 10.00] and will deduct 10.00 from the cost, so it becomes 0.00 - done. Can something like that be done to it? Because i dont see how.
• 05-03-2011, 12:03 AM
wookyoo
Use this for loop and let me know if it is what you want

for(int i = 0; i < avalChange.length; i++)
• 05-03-2011, 12:19 AM
ojn
That's what I said several hours ago. The only difference is I wanted the OP to think for themselves while you just spoon fed.
• 05-03-2011, 12:47 AM