0
votes

Its a tuck shop program!

public void sale() {
        if (!ingredients.isEmpty()) {
            printFood();
            String choice = JOptionPane.showInputDialog("Enter Your choices seperatad by a # to indicate quantity");
            String[] choices = choice.split(" ");
            String[] ammounts = choice.split("#");
            for (int i = 0; i < choices.length; i++) {
                int foodPos = (Integer.parseInt(choices[i])) - 1;
                int ammount = Integer.parseInt(ammounts[i+1]);
                try {
                    foods.get(foodPos).sale(ammount);
                } catch (IndexOutOfBoundsException e) {
                    System.out.println("Ingredient does not exsist");
                }
            }
        }


    }

http://paste.ubuntu.com/5967772/

gives the error

Exception in thread "main" java.lang.NumberFormatException: For input string: "1#3" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.lang.Integer.parseInt(Integer.java:527)
1

1 Answers

2
votes

You're splitting the same String twice, but Strings are immutable so you're getting back two different arrays while the original String stays the same. Therefore, if you have input like:

1#3 2#4

You splitting it with (" ") will yield:

1#3
2#4

Which you try to parse as an Integer later at this line:

int foodPos = (Integer.parseInt(choices[i])) - 1;

That is throwing a NumberFormatException. You need to re-split each individual array element with ("#"), rather than the source String.