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;
}
Gama
  • 352
  • 2
  • 16
  • It is totally unclear to me what you are asking. your English wording is very confusing. what is the issue? where is the error? – Kick Buttowski Sep 11 '14 at 04:15
  • Can you translate this for me? "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." – Kick Buttowski Sep 11 '14 at 04:15
  • lol, I copy and pasted the instructions just as I was told. But Im trying to find the words in a string (words) that have at least a certain amount of characters (min) and no more than certain amount of characters (max). hope that makes more sense – Gama Sep 11 '14 at 04:18
  • what is the issue here? – Kick Buttowski Sep 11 '14 at 04:20
  • my instructor gave test cases and there is something wrong with it according to those test cases so I was hoping to get some help – Gama Sep 11 '14 at 04:23
  • so tell me what is wrong? at least post up some error code? – Kick Buttowski Sep 11 '14 at 04:24
  • that us the thing, there is no error code except that showed by the test cases which is is that the output expected was 3 but 0 was given – Gama Sep 11 '14 at 04:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61019/discussion-between-kick-buttowski-and-gamaliel-tellez-ortiz). – Kick Buttowski Sep 11 '14 at 04:28

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,511
  • 28
  • 39
  • 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,709
  • 13
  • 37
  • 58