i could verify in my local all working good. would be great if some one would able to find bug . @codegym task should pass if provided example is passing, would be my suggestion.
package com.codegym.task.task22.task2209;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws IOException {
//reading a file from inputs
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//writing the debug on and off logic for testing locally or codegym site.
boolean debug = false; //setting initally value to true to debug locally.
String fileName; //string to save filename.
if (debug) {
fileName = "C:\\Users\\609631742\\OneDrive - BT Plc\\Notebooks\\or\\devops\\java-30-jan\\daily notes\\tet.txt";
} else {
fileName = reader.readLine();
}
FileInputStream fis = new FileInputStream(fileName);
//reading the file and saving contents to stringBuilder
Scanner sc = new Scanner(fis);
StringBuilder sb = new StringBuilder();
if (sc.hasNext()) {
sb.append(sc.nextLine());
}
while (sc.hasNextLine()) {
sb.append(" ");
sb = sb.append(new StringBuilder(sc.nextLine()));
}
//To verify if string Builder is displayed correctly.
// String stringInStringBuilder=sb.toString();
// System.out.println("Contents of StringBuilder: " + stringInStringBuilder);
// StringBuilder result = getLine();
// System.out.println(result.toString());
//To display the contents of string after splitting it.
// System.out.println("Splitting words with space and displaying to string: " + Arrays.toString(stringInStringBuilder.split(" ")));
StringBuilder result = getLine(sb.toString().split(" "));
System.out.println(result.toString());
}
public static StringBuilder getLine(String... words) {
//to verify the size of passed arguments in getLine method
//System.out.println("Displaying words size passed to getLine method: " + words.length);
//to verify if arguments passed in getLine method are correctly displayed as expected.
// System.out.println("Displaying words , which passed in getLine method for verification: ");
StringBuilder stringBuilderReturning = new StringBuilder(); //added to pass codegym verification.
//returning empty stringbuilder if arguments if null.
if (words.length == 0) {
return stringBuilderReturning;
}
//to get the size of words
int wordsLength = words.length;
LinkedList<String> stringLinkedList = new LinkedList<>();
//adding first word to List
stringLinkedList.add(words[0]);
//to check if there is any change in list and if no change break the loop.
int oldListSize = stringLinkedList.size();
int newListSize = 0;
//looping through untill linkedlist size is equal to words array size.
if (wordsLength != 0) {
while (oldListSize != newListSize) {
oldListSize = stringLinkedList.size();
//looping through all words in words array
for (int i = 0; i < wordsLength; i++) {
//System.out.println(words[i]);
//to check if word of array already exist in list , if yes then ignore.
if (!stringLinkedList.contains(words[i])) {
//to check if first letter of word equals to last letter of last word in list.
//getting the first and last characters of word in array and list in all lower case.
char firstCharInWords = Character.toLowerCase(words[i].charAt(0)); //first letter of word in word array
char lastCharInList = Character.toLowerCase(stringLinkedList.getLast().charAt(stringLinkedList.getLast().length() - 1)); //last letter of word in list
char lastCharInWords = Character.toLowerCase(words[i].charAt(words[i].length() - 1)); //first letter of word
char firstCharInList = Character.toLowerCase(stringLinkedList.getFirst().charAt(0));
//to check if first letter of word equals to last letter of last word in list.
if (firstCharInWords == lastCharInList) {
stringLinkedList.addLast(words[i]);
} else if (lastCharInWords == firstCharInList) {
stringLinkedList.addFirst(words[i]);
}
}
}
newListSize = stringLinkedList.size();
}
if (stringLinkedList.size() > 0) {
stringBuilderReturning.append(stringLinkedList.get(0));
}
for (int i = 1; i < stringLinkedList.size(); i++) {
stringBuilderReturning.append(" ");
stringBuilderReturning.append(stringLinkedList.get(i));
}
}
return stringBuilderReturning;
}
}