Home > OS >  Using startsWith and endsWith works, but how to get middle characters?
Using startsWith and endsWith works, but how to get middle characters?

Time:11-07

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

CodePudding user response:

Your code works correctly to check whether the substring is a prefix or suffix of a string but it fails when you try to check if a string contains a substring and the substring is neither a prefix nor a suffix.

The following if statement that you have used is wrong.

if (answer.contains(response)) {
     answer = answer   " - infix";
}

answer.contains(response) will be always true if the string contains the response. Even if the response is a prefix or a suffix.

The correct way to check if a substring is an infix of a string is the following:

if(str.contains(substring){ // First check if the string contains the substring
   if (!str.startsWith(subStr) && !str.endsWith(subStr)) { // Verify that the substring is neither a prefix nor a suffix
        answer = "-infix";
   }
}

Another mistake is that you are changing the original input string i.e inDictionary.nextLine() (stored in the variable answer) and appending the output string (i.e. whether the string is a prefix, suffix, or infix) to the same variable (that you have named as answer).

The correct way to check whether a substring is a prefix, suffix, or infix of a string is:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in); // Taking input from console 
    System.out.print("Enter a string: ");
    String str = scanner.nextLine();
    System.out.print("Enter a substring to find: ");
    String subStr = scanner.nextLine();
    System.out.println(findWord(str, subStr));
    scanner.close();   
}
//check for prefix, suffix or infix.
public static String findWord(String str, String subStr) {
   String answer = "";
   if (str.contains(subStr)) { //First check if the strings contains the substring
       if (str.startsWith(subStr)) {
           answer  = " -prefix";
       }
       if (str.endsWith(subStr)) {
          answer  = " -suffix";
       }
       if (!str.startsWith(subStr) && !str.endsWith(subStr)) {
          answer = "-infix";
       }
   } else
       answer = "not found";
   return answer;
}

findWord() method checks whether the string contains the substring.

  • Related