Advertisement
Vember

Beginner Code

Jun 19th, 2025 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner read = new Scanner(System.in);
  7.         //declare our lists for 'longest' and 'tooShort' variables
  8.         List<String> longest = new ArrayList<>();
  9.         List<String> tooShort = new ArrayList<>();
  10.         while (true) {
  11.             //take an entry as 'string' variable and split into 'split' string array
  12.             String string = read.nextLine();
  13.             String[] split = string.split(",");
  14.             //automatically index current entry to be removed later if proven invalid
  15.             //this also provides a "seed" for the loop during its first iteration
  16.             longest.add(split[0]);
  17.             //while loop ends if current entry is blank
  18.             if (string.equals("")) {
  19.                 break;
  20.             }
  21.             //check through each iteration of longest variable for if current entry is either greater than or less than
  22.             for (String s : longest) {
  23.                 if (split[0].length() > s.length()) {
  24.                     //current entry is greater than this 'longest' iteration, add iteration to 'tooShort' variable
  25.                     tooShort.add(s);
  26.                 } else if (split[0].length() < s.length()) {
  27.                     //current entry is less than this 'longest' iteration, add current entry to 'tooShort' variable
  28.                     //and break to avoid adding current entry to 'tooShort' variable multiple times unnecessarily
  29.                     tooShort.add(split[0]);
  30.                     break;
  31.                 }
  32.             }
  33.             //run through 'tooShort' iterations and remove each from 'longest' variable
  34.             for (String s : tooShort) {
  35.                 longest.remove(s);
  36.             }
  37.         }
  38.         //begin to print names
  39.         //check if there are multiple names with the longest length
  40.         boolean multipleNames = false;
  41.         if (longest.size() > 2) {
  42.             multipleNames = true;
  43.         }
  44.         if (!multipleNames) {
  45.             System.out.print("Longest name: ");
  46.         } else {
  47.             System.out.print("Longest names: ");
  48.         }
  49.         //if no multiple names determined, just print the only iteration of 'longest'
  50.         //otherwise print with ", " until second to last iteration
  51.         for (int i = 0; i < longest.size(); i++) {
  52.             if (!multipleNames) {
  53.                 System.out.print(longest.get(i));
  54.             } else if (i < longest.size() -2) {
  55.                 System.out.print(longest.get(i) + ", ");
  56.             } else {
  57.                 System.out.print(longest.get(i));
  58.             }
  59.         }
  60.     }
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement