My program take user input and compares it to words in a txt file. If it contains the user input such as "pre" in preload, it will print "prefix". If if contains "ful" like in helpful it will print "suffix". But there is also a result for infix, a letter within the word but not the start of end. I cannot get all 3 to work at the same time.
public static void substringProblem() throws FileNotFoundException {
String response;
String answer;
Scanner input = new Scanner(System.in);
System.out.println("Enter a substring: ");
response = input.next();
Scanner inDictionary = new Scanner(DICTIONARY);
for (int line = 1; line <= 23; line++) {
answer = inDictionary.nextLine();
if (answer.startsWith(response)) {
answer = answer + " - prefix";
}
if (answer.contains(response)) {
answer = answer + " - infix";
}
if (answer.endsWith(response)) {
answer = answer + " - suffix";
}
else if (!answer.contains(response)) {
answer = answer + " - not found";
}
System.out.println(answer);
}
Example output of "t"
tattarrattat - prefix - infix
absobloominglutely - infix
Example output of "na"
nana - prefix - infix
banana - infix