I was trying to make a program that prints out user-inputted values in an ArrayList, and for the most part, it works. Except it does not print the first element. Here is the code:
import java.util.Scanner;
import java.util.ArrayList;
public class Family {
public static void main(String[] args){
ArrayList<String> names=new ArrayList<String>();
Scanner in=new Scanner(System.in);
System.out.println("Enter the names of your immediate family members and enter \"done\" when you are finished.");
String x=in.nextLine();
while(!(x.equalsIgnoreCase("done"))){
x = in.nextLine();
names.add(x);
}
int location = names.indexOf("done");
names.remove(location);
System.out.println(names);
}
}
For example if, I enter jack, bob, sally, it'll print [bob, sally]
indexOf()andremove().names.remove("done")will do the trick.