I have been trying to get the last name in this list in order to output it using list.size()
. However, list.size()
only prints the number of names in an array.
Is there a simpler way to explain how I can only record the last name (which is Anna in this case) and print it out when while loop is broken/stopped?
The following names are inputted by the user using scanner utility:
John
Marry
Anderson
Anna
Scanner scanner = new Scanner(System.in);
ArrayList<String> listOfNames = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
System.out.println(listOfNames.size());
break;
}
listOfNames.add(input);
}