title: Scanner input and array duplicates verification causes an infinite loop
body: <p>I code a program that lists entered names. But if the entered name is already in the array, the user has to reenter a different name.</p>
  <p>I left a picture below that shows what is happening when I run the code</p>
  <pre class="lang-java prettyprint-override"><code>import java.util.Scanner;
  
  public class list {
      public static void main(String[] args) {
          int numberofhumans;
          Scanner al = new Scanner(System.in);
          System.out.print(&quot;Enter the number of person: &quot;);
          numberofhumans = al.nextInt();
  
          String[] list = new String[numberofhumans];
          for (int i = 0; i &lt; numberofhumans; i++) {
              System.out.print(&quot;Enter the name:&quot;);
              list[i] = al.nextLine();
  
              for (int j = 0; j &lt; i; j++) {
                  if (list[i] == list[j]) {
                      System.out.println(&quot;Name you just typed is already in your list. Enter a different name.&quot;);
                      i--;
                  }
              }
  
          }
  
          for (String string : list) {
              System.out.println(string);
          }
          al.close();
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/lbC2X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lbC2X.png" alt="1" /></a></p>