This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/3/7

How to approach a problem

JasonZhu JasonZhu

2014/3/7

#
public class WordFilter
{
    /**
     * Searches the data file and prints out the stats of the file.
     */
    public static void wordFilter(String[] arg)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the pattern you want to search for: ");
        String pattern = keyboard.nextLine();
        try{
            int matches = 0;
            Scanner in = new Scanner(new File("words.txt"));
            while(in.hasNextLine()) {
                String word = in.nextLine();
                word = word.trim();
            }
            System.out.println();
            if(matches == 0) {
                System.out.println("There are no matches.");
            } else {
                System.out.println("There are " + matches + " matches.");
            }
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }
    }
}
I would like to ask for your wisdom and approach to coding a word filterer. The input is composed of letters and asterisks (*). Example: b** would define a three letter word beginning with b. The above is what I have so far.
danpost danpost

2014/3/7

#
What is the format of your "words.txt" file? are there lines of text in it, or is there one word in each line? what are your concerns on upper v. lower case? are you looking for exact maching? or not? I will presume that (since the pattern defines the length of the word) patterns found inside larger words will be ignored and only upper and lowercase alpha characters will be allowed in the pattern (except for the asterick).
JasonZhu JasonZhu

2014/3/7

#
The format is similar to this: sat bat get gnat bologna And all is lowercase. The asterisks just help form the length of the word and do nothing else. The letters must be in the same position as the words found. Thanks for the quick reply!
danpost danpost

2014/3/7

#
I guess what kind of threw me off was the fact that you 'trim' the 'word' (which appears is probably not needed). What might actually be needed is 'toLowerCase' the 'pattern' because you do now know how the user will input the characters. First, I would compare the length to see if they were the same. If that check passed, then I would check the pattern using a 'for' loop to iterate through the length of the strings. If the pattern character is an asterick, then 'continue' the loop else compare check the characters at the current position using 'charAt'.
JasonZhu JasonZhu

2014/3/8

#
Thanks for sharing, danpost!
JasonZhu JasonZhu

2014/3/8

#
Can I ask how this would be done? : "If the pattern character is an asterick, then 'continue' the loop else compare check the characters at the current position using 'charAt'."
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the pattern you want to search for: ");
        String pattern = keyboard.nextLine().toLowerCase(); 
        try{
            int matches = 0;
            int counter = 0;
            Scanner in = new Scanner(new File("wordlist.txt"));
            while(in.hasNextLine()) {
                String word = in.nextLine();
                word = word.trim();
                if(word.length()==pattern.length()){
                    for(int i=0;i<pattern.length();i++){
                        if(word.charAt(i)==(pattern.charAt(i))){
                            System.out.print(word+"\t");
                            counter++;
                            matches++;
                            if(counter==5){
                                System.out.println();
                                counter=0;
                            }                            
                        }
                    }
                }
            }
            System.out.println();
            if(matches == 0) {
                System.out.println("There are no matches.");
            } else {
                System.out.println("There are " + matches + " matches.");
            }
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }
This is what I've gotten to so far.
danpost danpost

2014/3/8

#
JasonZhu wrote...
Can I ask how this would be done? : "If the pattern character is an asterick, then 'continue' the loop else compare check the characters at the current position using 'charAt'."
public boolean matchingStrings(String pattern, String word)
{
    if (word.length() != pattern.length()) return false;
    for (int n=0; n<pattern.length(); n++)
    {
        if (pattern.charAt(n) == '*') continue;
        if (word.charAt(n) != pattern.charAt(n)) return false;
    }
}
The two lines inside the 'for' block could be condensed to one line with the following:
if (pattern.charAt(n) != '*' && word.charAt(n) != pattern.charAt(n)) return false;
JasonZhu JasonZhu

2014/3/8

#
I had no idea that there was such thing as straight up 'continue';
danpost danpost

2014/3/8

#
JasonZhu wrote...
I had no idea that there was such thing as straight up 'continue';
It immediately goes to the next iteration of the loop without processing any code after it within the loop.
JasonZhu JasonZhu

2014/3/8

#
    public static void wordFilter()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the pattern you want to search for: ");
        String pattern = keyboard.nextLine().toLowerCase(); 
        try{
            int matches = 0;
            int counter = 0;
            Scanner in = new Scanner(new File("wordlist.txt"));
            while(in.hasNextLine()) {
                String word = in.nextLine();
                word = word.trim();
                if(matchingStrings(pattern,word)){
                    System.out.print(word+"\t");
                    counter++;
                    matches++;
                    if(counter==5){
                        System.out.println();
                        counter=0;
                    }                            
                }
            }
            System.out.println();
            if(matches == 0) {
                System.out.println("There are no matches.");
            } else {
                System.out.println("There are " + matches + " matches.");
            }
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }
    }

    /**
     * Checks the pattern and word to determine whether it is matching.
     */
    public static boolean matchingStrings(String pattern, String word)  
    {  
        if (word.length() != pattern.length()){
            return false;  
        }
        for (int n=0; n<pattern.length(); n++)  
        {  
            if (pattern.charAt(n) != '*' && word.charAt(n) != pattern.charAt(n)) {
                return false; 
            }
        }  
        return false;
    }
With the suggestion you've made, I've modified my code to the following. However, it does not work and I can't seem to see why. Suggestions please. Thanks a lot!
danpost danpost

2014/3/8

#
Your last line should be 'return true;'. Guess I forgot to add that line in my code above. Though, I was more interested in showing the use of 'continue' there.
JasonZhu JasonZhu

2014/3/8

#
Works well, thank you for your time and help danpost!
You need to login to post a reply.