java.lang.StackOverflowError

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dragonxpert
    New Member
    • Dec 2007
    • 2

    java.lang.StackOverflowError

    I seem to be having an overflow problem every time I plug in a value. The program is supposed to give the different combinations that a giving amount of money can be sorted into, with care given to the order, but every time I call the bestbills method, the program generates a StackOverflowEr ror. I'm not quite sure how to eliminate this problem.


    import java.lang.*;
    import java.util.*;
    import java.text.*;

    public class Money
    {
    public static DecimalFormat money = new DecimalFormat(" $0.00");
    public static final int DenomNum = 16;


    public static int bestbills (double[] value, boolean[] on, double cash)
    {
    //find the greatest number smaller than the cash allowed
    int greatest = DenomNum - 1;

    while (cash < value[greatest] && on[greatest])
    {
    greatest--;
    }

    //if the greatest value that can go into the cash is the lowest denomination,
    //it will return one if the cash is divisible by taht denomination, otherwise it
    //will return 0
    if (greatest == 0 && cash % value[greatest] == 0)
    {
    return
    1;
    }

    //finds out how many of the greatest value can go into the available cash
    int totin = 0;
    while (cash > value[greatest])
    {
    cash -= value[greatest];
    totin++;
    }

    return
    totin * bestbills(value , on, cash) + 1;

    }

    public static void main(String [] args)
    {
    final String SENTINEL = "exit";
    final String CoinChange = "change";
    final String Evaluate = "evaluate";
    double MoneyVal[] = new double[DenomNum];
    int combs;
    int MoneyNum[] = new int[DenomNum];
    boolean MoneyOn[] = new boolean[DenomNum];
    Scanner scan = new Scanner(System. in);
    int requi = 0;

    //Assign values of different denominations
    MoneyVal[0] = .01;
    MoneyVal[1] = .05;
    MoneyVal[2] = .10;
    MoneyVal[3] = .25;
    MoneyVal[4] = .50;
    MoneyVal[5] = 1;
    MoneyVal[6] = 2;
    MoneyVal[7] = 5;
    MoneyVal[8] = 10;
    MoneyVal[9] = 20;
    MoneyVal[10] = 50;
    MoneyVal[11] = 100;
    MoneyVal[12] = 500;
    MoneyVal[13] = 1000;
    MoneyVal[14] = 5000;
    MoneyVal[15] = 10000;

    //Set all Money present as true
    for (int i = 0; i < DenomNum; i++)
    {
    MoneyOn[i] = true;
    }

    String input;
    input = scan.next();
    scan.nextLine() ;

    while(input.com pareToIgnoreCas e(SENTINEL) != 0)
    {
    if(input.compar eToIgnoreCase(C oinChange) == 0)
    {
    if(scan.hasNext Int())
    {
    int change = scan.nextInt();
    scan.nextLine() ;
    if (change > -1 && change < DenomNum)
    {
    if (MoneyOn[change]) {
    MoneyOn[change] = false;
    } else {
    MoneyOn[change] = true;
    }
    }
    }
    } else if (input.compareT oIgnoreCase(Eva luate) == 0)
    {
    if (scan.hasNextDo uble())
    {
    double dbinput = scan.nextDouble ();
    scan.nextLine() ;
    double ulttot = 0;
    combs = bestbills(Money Val, MoneyOn, dbinput);
    for (int i = 0; i < DenomNum; i++)
    {
    ulttot += MoneyVal[i] * MoneyNum[i];
    requi += MoneyNum[i];
    }
    if (ulttot == dbinput)
    {
    System.out.prin tln("The best combination of monetary denominations for " +
    money.format(db input) + " is:");
    for (int i = 0; i < DenomNum; i++)
    {
    if (MoneyNum[i] != 0)
    {
    System.out.prin tln(MoneyVal[i] + " : " + MoneyNum[i]);
    }
    }
    System.out.prin tln("It requires " + requi + " piece(s) of currency.\n");
    }
    } else
    {
    System.out.prin tln("The given combination is impossible with the given " +
    "denominations. ");
    }
    }
    if (scan.hasNext() )
    {
    input = scan.next();
    scan.nextLine() ;
    }
    }
    }
    }
  • Dragonxpert
    New Member
    • Dec 2007
    • 2

    #2
    I was looking this over and realized that the method doesn't even do what it is intended to do. Back to the drawing board.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      A quick tip: multiply all your monetary units by 100, so you get 1, 2, 5, 10 etc.
      and use integers for those values. Double values don't go well with decimal fractions.

      kind regards,

      Jos

      Comment

      Working...