StefanTobler

TERM 2 Lesson 1

Jan 5th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.io.*;
  2. import static java.lang.System.*;
  3.  
  4. import java.util.Scanner;
  5. import java.lang.Math;
  6.  
  7. import java.util.ArrayList;
  8.  
  9.  
  10.  
  11. class Main{
  12. //Term 2 Lesson 1: ArrayList
  13.  
  14.      public static void main (String str[]) throws IOException {
  15.  
  16.        ArrayList<String> list  = new ArrayList<String> ();
  17.           Scanner scan = new Scanner(System.in);
  18.  
  19.           int flag = 0;
  20.           String temp = "error";
  21.           System.out.println("Please enter words, enter STOP to stop the loop.");
  22.           while (flag == 0)
  23.           {
  24.             temp = scan.nextLine();
  25.             if (temp.equals("STOP"))
  26.                 {
  27.                 flag = 1;
  28.                 break;
  29.                 }
  30.             list.add(temp);
  31.            
  32.           }
  33.          
  34.           System.out.println(list.size());
  35.           System.out.println(list);
  36.           if (list.size() > 2)
  37.           {
  38.             list.remove(list.size()-1);
  39.             list.remove(0);
  40.           }
  41.           System.out.println(list);
  42.      }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment