dimanche 1 mars 2015

Java: Misspell reporter using regex (how to parse for misspells)



I am creating a program that parses strings to report instances of misspells. I want it to report multiple instances opposed to one single variable. I had it interpret user input for instance;



GOOGGOUGGUIG



and take that string and report all instances where "GO" was spelled incorrectly 4 times because as seen in the above user entry we have "OG", "UG", "GU" and "IG".


So my result should be



Y was spelled incorrectly x/count times.



I don't care about the pattern reversal portion. I only used it to find instances for when I used the single variable.



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

public class misspellReporter
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String singleString = "";
System.out.println("Enter text here");
singleString = keyboard.nextLine();

String str = singleString;
//String strToSearch = "OG"; //I used this at first
String[] strToSearch = {"GU", "UG", "IG", "GI"}; //I want to use this array instead
String strToSearchReversed = new StringBuffer(strToSearch).reverse().toString();
Pattern strPattern = Pattern.compile(strToSearchReversed);
Matcher matcher = strPattern.matcher(str);
int counter = 0;
while(matcher.find()) {
++counter;
}

System.out.println(strToSearch+" was spelt as "+strToSearchReversed+" "+counter+" times");
}
}


Thank you ahead of time! The reason this question is different for me is because I haven't seen anyone else on the forum parse with matcher and patterns. I've used other methods but this one has a specific action to it that I take an interest in.




Aucun commentaire:

Enregistrer un commentaire