2

Im trying to write a method that returns the number of words from the "words" parameter that have at least the min" but no more than the "max c"haracters.

public static int countWords(String words, int min, int max)
{
    Scanner s = new Scanner (words);
    int counter = 0;

 while  (s.hasNext())
    {
    String word = s.next();
    int wordLength = word.length();
    if (wordLength>min && wordLength<max)
    {
        counter= counter + 1;
        s.close();
        }
    }
    return counter;
}

3 Answers3

1

Just a small change. You are closing the scanner in the while loop, move it out of the loop and it works.

public static int countWords(String words, int min, int max) {
        Scanner s = new Scanner(words);
        int counter = 0;
        while (s.hasNext()) {
            String word = s.next();
            int wordLength = word.length();
            if (wordLength > min && wordLength < max) {
                counter = counter + 1;
            }
        }
        s.close();
        return counter;
    }

And also I suggest, you change the if condition to make it inclusive of min and max. if (wordLength >= min && wordLength =< max) { For example countWords("count words in this line 1 22 333 4444 55555",3,4) would not return any result for any string with you current condition.

Akash
  • 46
  • 2
1

The preference of using split versus scanner while working with Strings has been discussed before in

I prefer regex, so here is a solution using the split method of String as follows:

Using regex

public class Sample {
    
    public static int countWords(String words, int min, int max) {

        String[] allWords = words.split("(?s)\\s+");
        int counter = 0;

        for (String word : allWords) {
            if(word.length() >= min && word.length() <= max)
                counter++;
        }
        return counter;
    }
    
    public static void main(String[] args) {
        String s = "find all the long words in this sentence";
        System.out.println(countWords(s,4,7));
    }
}

If you want to use Scanner, you can do it like this :

Using scanner

public class Sample {
    
    public static int countWords(String words, int min, int max) {

        Scanner scan = new Scanner(words);
        int counter = 0;
        int length = 0;

        while(scan.hasNext()){
            length  = scan.next().length();
            if(length >= min && length <= max)
                counter++;
        }
        scan.close();
        return counter;
    }
    
    public static void main(String[] args) {
        String s = "find all the long words in this sentence";
        System.out.println(countWords(s,4,7));
    }
}
Community
  • 1
  • 1
Infinite Recursion
  • 6,445
  • 28
  • 38
  • 51
0

Based on what I can understand from you, you want to count words that has specified max and min

For example,

"The rain in Spain falls mainly in the plain", 3, 5 is return 5 because they are 3 fives and 2 threes

your code is quite alright except this part which needs some change in its logic

if (wordLength>min && wordLength<max) 

Based on what I understand from your question you want word which has either min length or max length

As result you have to change that if statement to this

if (wordLength == min || wordLength == max) {

Explanation : length of the word is equal to min length or max length , counter is gonna be added

Kick Buttowski
  • 6,631
  • 13
  • 35
  • 57