# Thread: Double Array problem

1. Member
Join Date
May 2011
Posts
3
Rep Power
0

## 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:
Java 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!

2. Senior Member
Join Date
Sep 2008
Location
Stockholm, Sweden
Posts
119
Rep Power
0
The length of avalChange is 7.
Java 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

3. Member
Join Date
May 2011
Posts
3
Rep Power
0
If I do it this way:
Java 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:

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

Java 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:

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.
Last edited by AlinkaTroll; 05-03-2011 at 12:40 AM.

4. Member
Join Date
May 2011
Posts
7
Rep Power
0
Use this for loop and let me know if it is what you want

for(int i = 0; i < avalChange.length; i++)

5. Senior Member
Join Date
Sep 2008
Location
Stockholm, Sweden
Posts
119
Rep Power
0
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.
Last edited by ojn; 05-03-2011 at 01:23 AM.

6. Member
Join Date
May 2011
Posts
3
Rep Power
0
for(int i = 0; i < avalChange.length; i++)
That goes withot stop

7. Member
Join Date
May 2011
Posts
7
Rep Power
0

I think his comments are really important
for me too.

i'll try not to give exact code but give some hints from next time

#### Posting Permissions

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