Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my program freezing when I use a method? (Java)
    text
    copied!<p>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.</p> <p>The method is really simple and well-written, I'm not sure what's causing the problem.</p> <p>P.S. The method is on the bottom of the code.</p> <p>Thanks for your help!</p> <p>Edit: That was a dumb question now that I look at it. Thanks again everyone!</p> <pre><code>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 &lt;= stringArray.length; i++) { boolean itemExists = false; boolean AddItem = AddItem(); if (AddItem == true) { out.println("\nEnter a string"); String input = keyboard.next(); if (i &gt; 0) { for (int j = 0; j &lt; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload