Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level0.lesson0.task0;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- /* Самая короткая строка
- 1. Создай список строк.
- 2. Считай с клавиатуры 5 строк и добавь в список.
- 3. Используя цикл, найди самую короткую строку в списке.
- 4. Выведи найденную строку на экран.
- 5. Если таких строк несколько, выведи каждую с новой строки.
- */
- public class Solution {
- public static void main(String[] args) throws Exception {
- ArrayList<String> arrayList = new ArrayList<String>();
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- for (int i = 0; i < 5; i++) {
- arrayList.add(reader.readLine());
- }
- printIndexesByWidth(arrayList, getShortestLength(arrayList));
- }
- public static int getShortestLength(ArrayList<String> input) {
- int result = input.get(0).length();
- for (String element : input) {
- if (element.length() < result) {
- result = element.length();
- }
- }
- return result;
- }
- public static void printIndexesByWidth(ArrayList<String> input, int width) {
- for (String element : input) {
- if (element.length() == width) {
- System.out.println(element);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement