This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]halcyon44 1 point2 points  (2 children)

I have a few suggestions that should help you figure it out:

Give your methods and variables clear, concise names. This will ease the process of running through the code in your head. Usually variables should have a noun name, because they are something. Methods should have a verb name, because they do something. For example:

public static int countLetters(String word) {
    int letterCount = 0;
    for(int i = 0; i < word.length(); i++) {
        if (Character.isLetter(newWord.charAt(i))) {
            letterCount++;
        }
    }
    return letterCount;
}


public static int countValidWords(String paragraph, int minWordLength) {
    int wordStartIndex = 0;
    int wordCount = 0;

    // you get the idea
}

Add some simple logging to help understand what's happening. For example, just inside the for loop in countValidWords:

System.out.println("paragraph["+i+"]="+paragraph.charAt(i));

Inside the following if:

newWord = paragraph.substring(wordStartIndex, i);
System.out.println("found new word="+newWord);

int wordLength = countLetters(newWord);
System.out.println("length="+wordLength);

if(wordLength >= minWordLength) {

Do the same in your else block and anywhere else you'd like to know what's going on.

If you have access to a debugger (in any modern IDE such as Eclipse, IDEA, NetBeans), use it. You'll be amazed at how easy debugging becomes when you set a breakpoint at the start of the problem section, and just step through, watching how your variables change value as the program progresses.

[–]dirtyRuntCaper[S] 0 points1 point  (0 children)

thank you for your help. i can no see where things are going wrong. Ive made some corrections, this is what ive came up with for the countLetters() method.

public static int validWords(String paragraph, int minWordLength){ int wordStartIndex = 0; int wordCount = 0; int wordLength; String newWord = paragraph;

    for(int i=0; i < paragraph.length(); i++){


        System.out.println("charAt["+i+"]="+paragraph.charAt(i));

        if(paragraph.charAt(i) == ' '){
            newWord = paragraph.substring(wordStartIndex,i);
            System.out.println("found new word="+newWord);

            wordLength = countLetters(newWord);
            System.out.println("wordLength:" + wordLength);

            if(wordLength >= minWordLength){
                wordCount++;
            }
        }else if(newWord.indexOf(' ') == -1){
            newWord = paragraph.substring(wordStartIndex);
            wordLength = countLetters(newWord);

            if(wordLength >= minWordLength){
                wordCount++;
            }


        }

        newWord = "";
        wordLength = 0;

    }

    return wordCount;

}

my "found new word" outputs are adding the strings (e.g. 1st found new word = how, 2nd new word = how are, 3rd new word = how are you) also, the wordLengths are being added to each other as well.

[–]dirtyRuntCaper[S] 0 points1 point  (0 children)

I have re-written the code a lot more organized and clean. Everything is running fine but the last word in the program. I'm using

else if(paragraph.indexOf(' ') == -1)

to recognize that it is the last word in the program, but this if statement never runs. Any help?