0

I need help with getting the number of strings whose length is >= to a minlength given by the user. ex: the string input "this is a game" minlength = 2. the length of 3 words in that sentence is >= to minLength so the output should be 3. Since 3 words are >= the minLength

I facing a problem with the output. I inputing a string, splitting it apart into individual words and sending them to method which compute the output.. The desired for the above example is 3 but i m getting 1,1,1.

public class WordCount {

    /**
     * @param args
     */

    public static String input;
    public static int minLength;

    public static void Input() {

        System.out.println("Enter String: ");
        input = IO.readString();

        System.out.println("Enter minimum word length: ");
        minLength = IO.readInt();

    }

    public static void Calc() {

        Input();

        String[] words = input.split(" ");

        for (String word : words) {

            LetterCount(word);
        }

    }

    public static int LetterCount(String s) {


        int countWords = 0;


        if (s.length() >= minLength) {

            countWords += 1;

            IO.outputIntAnswer(countWords);

        }



        return countWords;

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Calc();

    }

}

3 Answers 3

1

You are very close!

You call LetterCount for each word, and at the start of LetterCount you set countWords to 0. Therefore, your counter is reset every time!

Have countWords not as a local variable to LetterCount, but a private variable in your class.

Place

private static int countWords = 0;

at the top of the file.

Remove

int countWords = 0;

from LetterCount.

Sign up to request clarification or add additional context in comments.

3 Comments

+1, but the field should be static since all the OP's methods are static. Also, the OP was returning the count from the method LetterCount so it is possible the OP was originally intending to consume the return value at the caller site and increment a similar count variable there. Finally, the call to print out the value IO.outputIntAnswer will have to be moved.
He's also calling the output statement at the wrong location. With only your changes, he'd get 1,2,3 out I believe
You're right as well. I'll leave that change as an exercise to the original poster. I'm sure he/she can figure it out from here.
0

Its because you're only reseting the countWords variable every time to zero and outputting 1. Make a static integer to hold the count, and call your output function in calc after you've literated over all the strings.

static int countWords = 0;
public static void Calc() {
    Input();

    String[] words = input.split(" ");

    for (String word : words) {
        LetterCount(word);
    }
    IO.outputIntAnswer(countWords);
}

public static int LetterCount(String s) {
    if (s.length() >= minLength) {
        countWords += 1;
    }
     return countWords;
}

Comments

0

You are setting countWords=0 every time you enter LetterCount() You should remove counting logic from LetterCount() and put it in Calc() around the loop.

2 Comments

Do tell why he should use a while loop? For loop its suited perfectly
Oh yeah, I actually ment the loop, whatever it is. Sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.