I've commented out this code as much a logically possible to make it easier to read.
I've polled the array to make sure it takes 10 strings , and that all 10 of them are stored in the array. (line 42)
I've tested and retested the build, it works to the requirements stated.
I've prodded around the help and discussion sections, and made small changes to exactly where I've placed things in the code.
I'm out of ideas. I could dismantle the logic methods, but I don't see how that would affect the parameter I'm missing.
Any help would be appreciated.
package com.codegym.task.task07.task0712;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
/*
Shortest or longest
1.X Create a list of strings.
2.X Add 10 strings from the keyboard.
3. Find out which string occurs earlier in the list: the shortest or the longest.
If several strings are shortest or longest, then consider the very first such string encountered.
4. Display the string described in Step 3. One string should be displayed.
Requirements:
1.X Declare a string list variable and immediately initialize it.
2.X The program should read 10 strings from the keyboard and add them to the list.
3.X The program should display the shortest string if it comes before the longest.
4.X The program should display the longest string if it comes before the shortest.
*/
public class Solution {
// Declare Arrays to pass longest/shortest logic
public static ArrayList<String> longest = new ArrayList<>();
public static ArrayList<String> shortest = new ArrayList<>();
public static void main(String[] args) throws Exception {
/* Initialize Keyboard reader
Declare & Initialize List
Populate List
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> strings = new ArrayList<>();
for (int i = 0; i < 10; i++) {
strings.add(reader.readLine());
}
//System.out.println(strings); //Test if array is populated to 10
getLongest(strings); //Pass array through logic
getShortest(strings); //
int longIndex = 0; //Initialize an integer to index longest string
int shortIndex = 0; //
// Assign a value to the longest and shortest strings index
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i) == longest.get(0)) {
longIndex = i;
}
if (strings.get(i) == shortest.get(0)) {
shortIndex = i;
}
}
// Determine which index comes first in array and display result
if (longIndex < shortIndex) {
System.out.println(strings.get(longIndex));
}
else {
System.out.println(strings.get(shortIndex));
}
}
// method to determine longest string in an array
public static void getLongest(ArrayList<String> strings) {
longest.add(strings.get(0));
//loop through list looking for longest string and place in (array)longest
for (int i = 0; i < strings.size(); i++ ) {
if (strings.get(i).length() > longest.get(0).length()) {
longest.set(0,strings.get(i));
}
}
}
// method to determine shortest string in an array
public static void getShortest(ArrayList<String> strings) {
shortest.add(strings.get(0));
for (int i = 0; i < strings.size(); i ++) {
if (strings.get(i).length() < shortest.get(0).length()) {
shortest.set(0, strings.get(i));
}
}
}
}