Advertisement
uhheeYt

Java Longest Word Couner

Jan 7th, 2022 (edited)
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. public static void printSix(@NotNull String input) {
  2.         String newInput = input.replace(".", "");
  3.         newInput = newInput.replace(":", "");
  4.         String[] inputArray = newInput.split(" ");
  5.         String longestWord = "";
  6.         long longestWords = 0;
  7.         for (String word : inputArray) {
  8.             if (word.length() < longestWord.length()) {
  9.                 continue;
  10.             }
  11.             longestWord = word;
  12.             longestWords = longestWords + 1;
  13.         }
  14.         if (longestWords != 1) {
  15.             System.out.println("Error. Multiple long words found. Please try again.");
  16.         } else {
  17.             System.out.println(longestWord + ", Length: " + longestWord.length());
  18.         }
  19.     }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement