This the output from the terminal. I've created another arraylist as the original arraylist will not maintain the same sequence in which numbers were added. Earlier without second arraylist I was getting same errors. I've debugged the program with various inputs but I'm not able to pass through the verification
Hippo
Tiger
Kangaroo
Giraffe
Goat
cat
Lion
Rabbit
Deer
Fox
9
3
Kangaroo
package com.codegym.task.task07.task0712;
import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Shortest or longest
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
int arrString[] = new int[10];
int indexMax=0;
int indexMin=0;
InputStream inputStream = System.in;
Reader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
ArrayList<String> strings = new ArrayList<String>();
ArrayList<String> stringsFwd = new ArrayList<String>();
for (int i = 0; i<10; i++){
strings.add(0, bufferedReader.readLine());
}
int j =strings.size()-1;
for (int i = 0; i<strings.size(); i++){
stringsFwd.add(i, strings.get(j));
j--;
}
for(int i = 0; i<stringsFwd.size(); i++){
arrString[i]= stringsFwd.get(i).length();
}
indexMax=arrString[0];
indexMin=arrString[0];
// for finding out max and min length of string
for (int i =1; i<arrString.length; i++){
if (arrString[i]>indexMax){
indexMax=arrString[i];
}
if (arrString[i]<indexMin){
indexMin=arrString[i];
}
}
// Printing the strings in the order they were added from terminal
for(int i = 0; i<stringsFwd.size(); i++){
System.out.println(stringsFwd.get(i));
}
System.out.println(indexMax);
System.out.println(indexMin);
//printing longest and Shortest string
for(int i = 0; i<stringsFwd.size(); i++){
if (stringsFwd.get(i).length()==indexMax){
System.out.println(stringsFwd.get(i));
break;
}
else if (stringsFwd.get(i).length()==indexMin){
System.out.println(stringsFwd.get(i));
break;
}
}
}
}