I suppose I completed the task, but not passed the validation. Why?
package com.codegym.task.task22.task2209;
/*
Make a word chain
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
String str = "";
BufferedReader rd = new BufferedReader(new FileReader(fileName));
while(rd.ready()){
str += rd.readLine();
}
String[] strings = str.split(" ");
StringBuilder result = getLine(strings);
System.out.println(result.toString());
}
public static StringBuilder getLine(String... words){
ArrayList<String> list = new ArrayList<>();
ArrayList<String> copy = list;
StringBuilder sb = new StringBuilder("");
if(words.length == 0) return sb;
if(words.length == 1) return sb.append(words[0]);
list.addAll(Arrays.asList(words));
//System.out.println(list);
String s1 = "";
String s2 = "";
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < copy.size(); j++) {
s1 = list.get(i);
s2 = copy.get(j);
if(s1.charAt(s1.toLowerCase().length() - 1) == (s2.toLowerCase().charAt(0)) && i != j) {
sb = new StringBuilder();
sb.append(s1).append(" ").append(s2);
copy.add(sb.toString());
copy.remove(s2);
copy.remove(s1);
list = copy;
i = 0;
break;
}
if(s2.charAt(s2.toLowerCase().length() - 1) == (s1.toLowerCase().charAt(0)) && i != j){
sb = new StringBuilder();
sb.append(s2).append(" ").append(s1);
copy.add(sb.toString());
copy.remove(s2);
copy.remove(s1);
list = copy;
i = 0;
break;
}
//System.out.println(list);
}
}
//System.out.println(list);
//System.out.println(sb.toString());
//words = list.toArray(new String[0]);
//sb = new StringBuilder();
//for (String s : words){
//if(s.contains(" ")) sb.append(s);
//}
if(list.size() > 1) sb = new StringBuilder();
return sb;
}
}