0

I am very close to finishing my task, but I can't figure out how to call findChange() correctly. My guess is that it needs to be in the main method. But when findChange(); call it, it asks for int, List<Integer>, List<Integer> so how do I do this "correctly" so to speak.

CODE

import java.io.*;
import java.util.*;
import java.lang.*;

public class homework5 {

    public static int change;

    public static void main(String[] args)
        throws FileNotFoundException 
    { //begin main

        ArrayList<Integer> coinTypes = new ArrayList<Integer>();//array to store
        //coin types

        Integer i;
        File f = new File (args[0]);
        Scanner input = new Scanner(f); //initialize scanner
        input.nextLine();
        while(input.hasNextInt()) {
            i = input.nextInt();
            coinTypes.add(i);
        }
        change = coinTypes.get(coinTypes.size()-1); //this will add all ints
        coinTypes.remove(coinTypes.size()-1);
        System.out.println("Found change"); //used for debugging
        System.out.println("Change: " + change);

        //findChange(); ideal spot to call the method
        //System.out.println(coinTypes);
    }

    boolean findChange(int change, List<Integer> coinTypes,
                       List<Integer> answerCoins) 
    { //contains means of
        //finding the change solutions
        if(change == 0) {
            return true; //a solution
        }
        if(change < 0) {
            return false; //if negative it can't be a solution
        } else {
            for(Integer coin : coinTypes) {
                if(findChange(change - coin, coinTypes, answerCoins)){
                    answerCoins.add(coin); //if it works out add it to the
                    return true;         //solution List
                }
            }

        }

        List<Integer> answer = new ArrayList<Integer>();
        boolean canFindChange = findChange(change, coinTypes, answer);
        if(canFindChange) { //if there is a solution, print it
            System.out.println(answer);
        } else { System.out.println("No change found");
        }
        return false; //else return false
    }

}

This program calculates all the different ways to show change for a certain amount of money ie: 143 ($1.43). All I gotta do is call findChange() to main and it should work, what am I missing?

EDIT I just realized I didn't specify the method call I need help with I apologize for any unclearness

INPUT FILE

// Coins available in the USA, given in cents.  Change for $0.09?
1 5
9

CURRENT OUTPUT

Change: 9

WANT

Change: 9
['solutions to all possible combinations to make $0.09']
Naetmul
  • 14,544
  • 8
  • 57
  • 81
Junikin
  • 301
  • 2
  • 14
  • 3
    Unrelated to your question but you should declare your constants with [`final`](http://en.wikipedia.org/wiki/Final_%28Java%29) keyword. – aug Nov 24 '14 at 06:50
  • Ok we can revisit as soon as this first part is done – Junikin Nov 24 '14 at 06:51
  • It would also *really* help if you'd indent your code properly - it's very hard to read at the moment. – Jon Skeet Nov 24 '14 at 06:56
  • Thank you @Jason C and I apologize to everyone for any confusion – Junikin Nov 24 '14 at 06:59
  • There seems to be a whole bunch of problems here. I'm not sure why you've declared those coin types (nickel, etc.) but decided to read them in from a file. I would just use `coinTypes.add(penny);' 'coinTypes.add(nickel);', etc. – Persixty Nov 24 '14 at 07:01
  • It's not clear why you're recursing again near the bottom: `findChange(change, coinTypes, answer)`. What is that meant to achieve? (You haven't modified `coinTypes` or `change`, so surely it's just going to go through the same steps again... if it can't find an answer, it'll recurse forever until you get a stack overflow.) – Jon Skeet Nov 24 '14 at 07:01
  • @Junikin I'm a little unclear on what your actual problem is, but I suspect you may want to read [the official tutorial on passing information to a method](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html). It is concise and well-written and should give you some ideas. – Jason C Nov 24 '14 at 07:01
  • Next the comment `//this will add all ints' isn't right. That line will get the last entry in the array. The following line will remove it. – Persixty Nov 24 '14 at 07:04
  • I know there are a few problems I am just a little desperate about getting the correct method to work because I really can't figure it out I was hoping for some explanation or help I know I don't have the best question, or concise code, but I just really need some assistance – Junikin Nov 24 '14 at 07:07
  • PS: Sorry to spam your comments but as I said, you've got a number of different problems and comments are quite cramped. – Persixty Nov 24 '14 at 07:08
  • No big deal I understand but as long as it works thats all that matters to me, but continuing, I think I see what you're saying, but how does this solve my method call issue? Am I going at this right way? I am really new to java and this recursive concept – Junikin Nov 24 '14 at 07:13
  • Apologies. I misread. What will happen is that it will only every return {1,1,1,1,..} up to the value of the change! It will 'trial' a penny. That will work for all values down to 0 and it will return `true` against that. I can tell you're new. But you're quite close. Fix up the main method and call `findChange(17,coinTypes,answerCoins);' where answerCoins is a new array and look what's inside it. – Persixty Nov 24 '14 at 07:20
  • I'm not quite sure what you mean by 'fix up the main method' where? and since this program reads in a file, putting in 17 won't help much because its supposed to use `change` which equals the last int in the file my tester will be using. I will update with the file I use so you can see what I mean – Junikin Nov 24 '14 at 07:25
  • In that file change = 143, so the program will find all the change solutions for $1.43 – Junikin Nov 24 '14 at 07:27
  • @Junikin If you are unsure how to pass parameters to methods, read the tutorial I linked to. – Jason C Nov 24 '14 at 07:28
  • @JasonC Thank you I am looking at it now. I do have a basic understanding of it, thats why I am confused on why my way isn't working – Junikin Nov 24 '14 at 07:33
  • OK. I'm starting to get it. I still want you to put a small number in for now. There are loads and loads of 'answers' for 143. I was confused by your nickel and dime constants. I thought they were important and you'd forgotten to use them. – Persixty Nov 24 '14 at 08:00
  • 11 cents will be interesting enough. I think want various combinations of 1,5 & 10 to come back. 11*1, 6*1 + 5 , 1 + 10, 1 + 2*5 – Persixty Nov 24 '14 at 08:03
  • I do have a file with the change = 9 but all I get back as an output (without calling `findChange();`) is `change: 9` which is supposed to happen but it doesn't show the solutions and THAT'S the problem, why I asked this question – Junikin Nov 24 '14 at 08:10

0 Answers0