My code won't pass the last two requirements, please have a look at my code and let me know if you have any suggestions. Thanks!!
In the main method, read the name of a file that contains words separated by spaces from the console.
In the getLine method, use StringBuilder to order all the words as follows: the last letter of a word must match the first letter of the next word (ignore case).
Each word should be used once.
The getLine method can return any variant.
Separate words with a space.
Display the resulting string on the screen.
Example input file:
OkinawaWashingtonAucklandKalamazooNorfolk
Result:
WashingtonNorfolkKalamazooOkinawaAuckland
Requirements:
The main method must read the filename from the keyboard.
There should not be static fields in the Solution class.
The getLine method must use StringBuilder.
The getLine method must return an empty string (an empty StringBuilder) if it is not passed arguments (words).
The getLine method must not change the arguments (words).
All words passed to the getLine method must be included in the resulting string, if possible.
This task has caused a lot of problems solving, mostly due to the ambiguous task conditions. I can share with you the difference between the code that I passed this task with and the code you have above:
My code will return a valid chain of 2 or more words as long as at least 2 words in the input can be chained together. This is because it checks the leading word and when that word can't be chained to any other it will skip it and move to the next until it finds one where a chain can start. Only when no possible chain(s) can be formed from any of the given words will it return one of them. The above code starts the chain from the first word, and if no word can chain to that word then it just returns it. For example, if the file contained:
People Okinawa Washington Auckland Kalamazoo Norfolk
My code returns "Washington Norfolk Kalamazoo Okinawa Auckland", however the above code just returns "People". If the word People existed in any other location in the file instead of the first word then the above code will return the same as my code.
I can't tell you if this will make your code pass or not, but it is a difference between our respective codes. I have heard from other people that have passed that they solved it completely different than I did, so I can't tell you 100% what will work and what will not.