2

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!

Michael Wendel
  • 143
  • 2
  • 5
  • 12

4 Answers4

2

Change scan.nextLine() != "STOP" to while(!scan.nextLine().equalsIgnoreCase("stop")); and try.

Reason :

The String Literal "STOP" will not be be same as the String value "STOP" entered from the keyboard (in your case). == compares references. You have to check value of 2 Strings not references.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Thank you, this seems to work, however near the end when I'm trying to print out the ArrayList, it only prints out the element in the middle of the ArrayList. Do you have any clue as to why this is happening? Nevermind, the comment below explained what I was doing wrong. Thank you though! – Michael Wendel Jan 22 '15 at 06:25
2

In line:

scan.nextLine() != "STOP"

you compare references to two objects. If you need to compare objects you should use equals() method of Object.

Read about equals() in the documentation.

But there is another problem in your code. You read next line twice. In loop and in while(...) statement.

Try this:

System.out.println("What would you like to add to the list?");
String input = scan.nextLine();
while(!input.equals("STOP"))
{
    list.add(input);
    input = scan.nextLine();
}
Vitaly
  • 2,552
  • 2
  • 18
  • 21
2

Try the below code:-

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);

    System.out.println("What would you like to add to the list?");
    String input = scan.nextLine();

    while (!input.equals("STOP")) { // compare string using equals(Object o) method
      list.add(input);

      System.out.println("What would you like to add to the list?");
      input = scan.nextLine();
    }

    System.out.println("Size of list = " + list.size());

    System.out.println("Input list:-\n" + list);

    if (list.size() > 2) {
      list.remove(0);
      list.remove(list.size() - 1);

      System.out.println("List after removing first and last eliment:-\n" + list);
    }
  }

}
shalin
  • 25
  • 3
0
import java.io.*;
import static java.lang.System.*;

import java.util.Scanner;
import java.lang.Math;

import java.util.ArrayList;

public class U7_L1_Activity_One{

   public static void main (String str[]) throws IOException {
   ArrayList<String> list = new ArrayList<String>();

        Scanner scan = new Scanner(System.in);  

    System.out.println("Please enter words, enter STOP to stop the loop.");
    String input = scan.nextLine();

    while (!input.equals("STOP")) { // compare string using equals(Object o) method
      list.add(input);
      input = scan.nextLine();
       }

    System.out.println(list.size());

    System.out.println(list);
    
  //  System.out.println("Size of list = " + list.size());

    //System.out.println("Input list:-\n" + list);

    if (list.size() > 2) {
      list.remove(0);
      list.remove(list.size() - 1);
      System.out.println(list);
      
    //  System.out.println("List after removing first and last eliment:-\n" + list);
    }
      System.out.println(list);
  }

}
Eric Blum
  • 744
  • 12
  • 29