开发者

How can write this? I Need help specifially with my nextWord method

How can write this? I Need help specifially with my nextWord method

package code;

import java.util.HashMap;

public class WordCounter {

    /**
     * Reads a file (identified by inputFilePath), one character at a time, and tracks
     * word counts using a java.util.HashMap<String,Integer>. 
     * 
     * A word is defined as a contiguous sequence of characters that does not contain
     * word separator characters, where word separator characters are: ' ', '\t', '\n',
     * ',' and '.'
     *   
     * You may use only CharacterFromFileReader to read characters from the input file. 
     * 
     * In order to keep your code readable, break your code into several methods.  Only 
     * the wordCounts method may be public; define meaningful private helper m开发者_高级运维ethods that 
     * you call from the wordCounts method.
     * @returns a HashMap containing the word->count mappings
     */

    public HashMap<String,Integer> wordCounts(String inputPath) {

        return new HashMap<String, Integer>();
    }

    private String nextWord(CharacterFromFileReader iterator) {

        while(iterator.hasNext()){ 
//make loop that takes each word an saves it to a string until u hit a space break 

        String s = "";

        if(iterator.hasNext()){

    }   
        }
    return null;
    }
        } 


How about this?

String words[] = str.split("[ ,\\t\\n,\\.]");

To generate str you can use the append operation to a StringBuilder in your iterator loop.


I would use a StringBuider in your nextWord(). Then you can iterate until you reach a word separator character or the end of the stream, and return that word. It should go something like:

StringBuilder sb = new StringBuilder();
char nextChar;
while(iterator.hasNext()) {
    nextChar = iterator.next();
    switch(nextChar) {
        case ' ':
        case '\t':
        case '\n':
        case ',':
        case '.':
            return sb.toString();
    }
}


I think you might be approaching the problem of defining nextWord the wrong way. It looks like you've filled out a frame for the nextWord function without understanding HOW the function should accomplish what it's going to do. This is backwards. Try following these basic steps:

  1. Write down in English exactly what nextWord should do. Think about any edge cases. What should it return given a sentence? What should it return if there are no characters left in CharacterFromFileReader?

  2. Once you have this definition write down(in English) step by step instructions for how to accomplish the task. e.g. 1) Get next character 2) If character is white space 3)etc...

  3. Try following the instructions yourself using pen and paper. Do they work? If not edit and repeat this step until they do.

  4. Now that you know HOW the function should work, implement the instructions you've written down using java code


I am guessing that the frame for the nextWord()-method was given by the teacher, and if OP would deliver something involving StringBuilder/StringBuffer teacher might scream "we haven't covered that yet". Also, teacher seems very happy with inefficient object-created-for-every-character monstrosities, so I think OP should go with using Strings-only; as in: inefficient but allowed by teacher:

private String nextWord(CharacterFromFileReader iterator) {
    String toReturn = "";
    while(iterator.hasNext()){ //make loop that takes each word an saves it to a string until u hit a space break 
            String s = "";
            if(iterator.hasNext()){
                    char c = iterator.next();
                    if(c==' ') break;
                    toReturn = toReturn+c;
            }
    }
    return toReturn;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜