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,getLengthLongest (arrayList));
- }
- public static int getLengthLongest (ArrayList<String> input) {
- int result = 0;
- for (int i = 0; i < input.size (); i++) {
- if (result < input.get (i).length ()) {
- result = input.get (i).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