SHARE
TWEET

Untitled

svetlyo0659 Feb 21st, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5. public class FiveNamesArrayList
  6. {
  7.   public static void main(String[] args)
  8.   {
  9.     ArrayList<String> names = new ArrayList<>();
  10.  
  11.     Scanner in = new Scanner(System.in);
  12.     System.out.println("Please enter at least five random names:");
  13.     String name = in.nextLine();
  14.  
  15.     while (true) {  // infinite loop
  16.       names.add(name);
  17.       if (name.equals(".")) {
  18.         if (names.size() < 5) {
  19.           System.out.println("You need at least five names!");
  20.           System.out.println("Enter a name:");
  21.           names.remove(name);
  22.         }
  23.         else {
  24.           names.remove(name);
  25.           break;
  26.         }
  27.       }
  28.       name = in.nextLine();
  29.     }
  30.  
  31.     System.out.println(names.get(4));
  32.     Collections.sort(names); ///sorts the ArrayList. in  alphabetical order
  33.     System.out.println(names);
  34.   }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top