Why is my program freezing when I use a method? (Java)

Posted by user2915567 on Stack Overflow See other posts from Stack Overflow or by user2915567
Published on 2013-10-26T09:41:41Z Indexed on 2013/10/26 9:54 UTC
Read the original article Hit count: 226

Filed under:
|
|
|

When I use a boolean method in the Main body, my program freezes and stops working. I've tried putting the method at different places but the exact same thing happens - it freezes.

The method is really simple and well-written, I'm not sure what's causing the problem.

P.S. The method is on the bottom of the code.

Thanks for your help!

Edit: That was a dumb question now that I look at it. Thanks again everyone!

public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int stringNumber = 0;
        String[] stringArray = new String[10];


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

            boolean itemExists = false;
            boolean AddItem = AddItem();


            if (AddItem == true) {

                out.println("\nEnter a string");
                String input = keyboard.next();

                if (i > 0) {
                    for (int j = 0; j < stringArray.length; j++) {
                        if (input.equalsIgnoreCase(stringArray[j])) {
                            itemExists = true;
                            out.println("Item \"" + input + "\" already exists.");
                            break;
                        }
                    }
                }

                if (itemExists == false) {
                    stringArray[stringNumber] = input;
                    out.println("\"" + stringArray[stringNumber] + "\"" + " has been stored.\n");
                } else {
                    out.println("Try again.");
                    i--;
                }

                PrintArray(stringArray);
                stringNumber++;
            }
        }
    }

  // This is the method I was talking about //
    public static boolean AddItem() {
        Scanner keyboard = new Scanner(System.in);
        int input = keyboard.nextInt();
        out.println("If you want to add an item, Press 1");

        if (input == 1) {
            return true;
        } else {
            out.println("Invalid input.");
            return false;
        }
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about function