import java.util.*;
public class Test {
public static void main(String[] args) {
ArrayList<String> name = new ArrayList<String>();
String cond="y";
do {
Scanner n = new Scanner(System.in);
System.out.println("enter the name: ");
String value = n.nextLine();
name.add(value);
Iterator itr = name.iterator();
while(itr.hasNext()) {
System.out.println("Names on the list: "+itr.next());
}
System.out.println("Want to continue? (Y/N): ");
cond=n.nextLine();
} while(cond=="Y");
}
System.out.println("End of Program");
}
This is the code to get the Names and store them in the list. Then it must print all those values stored in the list. Then it must ask the user whether they have some more input, if "yes" then it must get the value, store it, display all names.
But it is not working in the while loop. Can anyone explain me this?
Expected output:
Enter the name: xyz
Names on the list: xyz
Want to continue? (Y/N): Y
Enter the name: abc
Names on the list: xyz,abc
Want to continue? (Y/N): N
End of Program