I need to select all words of the maximum length and all of the minimum length.
For example:
When I was younger, so much younger than today
I never needed anybody's help in any way
But now these days are gone, I'm not so self-assured
Now I find I've changed my mind
I've opened up the doors
Example of the Output:
Min: I, s, m
Max: younger, anybody, assured, changed
I already figured out the algorithm, but it simply terminates after the sorting loop (with an int k), and I can't why, because the debugger says nothing because of my unit testing. It simply stops. Could you help me to figure out why it doesn't work pls? P.S. I can't use container classes
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Part2 {
public static void main(String[] args) {
String input = Util.getInput("part2.txt");
System.out.print(convert(input));
}
public static String convert(String input) {
Pattern p = Pattern.compile("[A-zА-я]+");
Matcher m = p.matcher(input);
int i = 0;
String[] arr = new String[100];
while (m.find()){
arr[i] = m.group(0);
i++;
}
StringBuilder longestResult = new StringBuilder();
longestResult.append("Max: ");
StringBuilder shortestResult = new StringBuilder();
shortestResult.append("Min: ");
int longest = 0;
int shortest = arr[0].length();
for (int k = 0; k < arr.length; k++){
if (arr[k].length() > longest){
longest = arr[k].length();
}
if (arr[k].length() < shortest){
shortest = arr[k].length();
}
}
for ( String word : arr) {
if (word.length() == longest) {
longestResult.append(word + ", ");
}
if (word.length() == shortest) {
shortestResult.append(word + ", ");
}
}
return shortestResult.substring(1, shortestResult.length() - 2) +
longestResult.substring(1, longestResult.length() - 2);
}
}