Advertisement
FedchenkoIhor

самая короткая строка

Apr 26th, 2016
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package com.javarush.test.level0.lesson0.task0;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6.  
  7. /* Самая короткая строка
  8. 1. Создай список строк.
  9. 2. Считай с клавиатуры 5 строк и добавь в список.
  10. 3. Используя цикл, найди самую короткую строку в списке.
  11. 4. Выведи найденную строку на экран.
  12. 5. Если таких строк несколько, выведи каждую с новой строки.
  13. */
  14. public class Solution {
  15.     public static void main(String[] args) throws Exception {
  16.         ArrayList<String> arrayList = new ArrayList<String>();
  17.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.         for (int i = 0; i < 5; i++) {
  19.             arrayList.add(reader.readLine());
  20.         }
  21.         printIndexesByWidth(arrayList, getShortestLength(arrayList));
  22.     }
  23.  
  24.     public static int getShortestLength(ArrayList<String> input) {
  25.         int result = input.get(0).length();
  26.         for (String element : input) {
  27.             if (element.length() < result) {
  28.                 result = element.length();
  29.             }
  30.         }
  31.         return result;
  32.     }
  33.  
  34.     public static void printIndexesByWidth(ArrayList<String> input, int width) {
  35.         for (String element : input) {
  36.             if (element.length() == width) {
  37.                 System.out.println(element);
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement