Using Matcher to find a word or a phrase in user Input in Java

213 Views Asked by At

It's a simple, so to say, text editor for cmd. The user pastes in text they want to edit and then choose one of the 7 options. One of them (7) is looking for a specific word/phrase. The problem is, that after pressing 7 the program stops executing so the user can't write the word they're looking for. The console doesn't show me any errors.

    import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class TextEd {
    
    public static void main(String[] args) {
        
        Editor editor = new Editor();
        editor.copiedText();
        }
    }
    
    class Editor {
        
        private Scanner scan = new Scanner(System.in);
        private String text = " ";
        
        public void copiedText() {
        
        System.out.println("Paste your text here.");        //The user input
        text = scan.nextLine();
        menu();
    }

public void menu() {
    
    System.out.println("Welcome to the tect editor.\n"
    + "What do you wish to do?\n"
    + "1. Count characters.\n"
    + "2. Put text to upper case.\n"
    + "3. Put text to lower case.\n"
    + "4. Put text backwards.\n"
    + "5. not yet.\n"
    + "6. not yet.\n"
    + "7. Search in text.\n"
    + "8. Exit program.");
    int choice = scan.nextInt();
    
    if (choice == 1) {
        counting();
    }
    else if (choice == 2) {
        bigLetters();
    }
    else if (choice == 3) {
        smallLetters();
    }
    else if (choice == 4) {
        backward(); 
    }
    else if (choice == 5) {
        searching();
    }
    else if (choice == 8) {
        System.exit(0);
    }
}

public void counting() {
    System.out.println("Character count: " + text.length());
    menu();
}

public void bigLetters() {
    System.out.println(text.toUpperCase());
    menu();
}

public void smallLetters() {
    System.out.println(text.toLowerCase());
    menu();
}

public void backward() {
String source = text;
    
    for (String part : source.split("  ")) {
        System.out.println(new StringBuilder(part).reverse().toString());
        System.out.print("  ");
    }
    menu();

}
public void searching() {
    String source = text;
    String word = scan.nextLine();
    System.out.println("What word or phrase do you wish to find?"); //Here the user writes a word or a phrase they want to find
    scan.nextLine();
    String patternString = word;
    
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(text);
    
 
    while(matcher.find()) {

        System.out.println(matcher.start() + matcher.end());   
    }
}

}

1

There are 1 best solutions below

1
On

Well, you do not call searching() after pressing 7 but with 5, see your code:

else if (choice == 5) {
    searching();
}

You should consider using a switch case for your code to improve readability.

int choice = scan.nextInt();

switch (choice)
case 1:
    counting();
    break;
case 2:
    bigLetters();
    break();
case 3:
    smallLetters();
    break;
case 4:
    backward(); 
    break;
case 5:
    searching();
    break;
default:
    System.exit(0);
}