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.
I left a picture below that shows what is happening when I run the 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("Enter the number of person: ");
numberofhumans = al.nextInt();
String[] list = new String[numberofhumans];
for (int i = 0; i < numberofhumans; i++) {
System.out.print("Enter the name:");
list[i] = al.nextLine();
for (int j = 0; j < i; j++) {
if (list[i] == list[j]) {
System.out.println("Name you just typed is already in your list. Enter a different name.");
i--;
}
}
}
for (String string : list) {
System.out.println(string);
}
al.close();
}
}