So I'm trying to write a Java program that allows a user to input words at the command line. The program should stop accepting words when the user enters "STOP". Store the words in an ArrayList. The word STOP should not be stored in the list.
Next, print the size of the list, followed by the contents of the list.
Then, remove the first and last words stored in the list, but only if the list has a length greater than two. Finally, reprint the contents of the list.
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class t2_lesson1_template {
public static void main (String str[]) throws IOException
{
ArrayList<String> list = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
do
{
System.out.println("What would you like to add to the list?");
String input = scan.nextLine();
list.add(input);
}
while( scan.nextLine() != "STOP");
if ( list.size() < 2)
{
System.out.println(list);
System.out.println(list.size());
}
else
{
list.remove(0);
list.remove(list.size()-1);
System.out.println(list);
System.out.println(list.size());
}
}
}
It keeps on prompting the question, but never recognizes when "STOP" is the input. If somebody could please help me figure out what's wrong, it'd help a lot. Thank you!