Results 1 to 7 of 7
  1. #1
    AlinkaTroll is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Unhappy 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. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    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. #3
    AlinkaTroll is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    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-02-2011 at 11:40 PM.

  4. #4
    wookyoo is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Use this for loop and let me know if it is what you want

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

  5. #5
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    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 12:23 AM.

  6. #6
    AlinkaTroll is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    for(int i = 0; i < avalChange.length; i++)
    That goes withot stop

  7. #7
    wookyoo is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    yeah.. read ojn's comments

    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

Similar Threads

  1. how to stop decimal in a double array
    By Billaguana in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 01:07 AM
  2. returning double array
    By Billaguana in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 03:59 AM
  3. Array of 4 bytes to double.
    By blind melon in forum Advanced Java
    Replies: 4
    Last Post: 12-08-2010, 11:19 AM
  4. Converting double to char array?
    By Blasz in forum New To Java
    Replies: 9
    Last Post: 08-29-2010, 07:32 PM
  5. manipulation in double dimensional array
    By akulkarni1234 in forum New To Java
    Replies: 1
    Last Post: 07-19-2009, 07:37 PM

Posting Permissions

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