0

I have written the below code to calculate the number of words for a given string. However, it does not account for whitespaces. How would I implement Character.isWhitespace(text.charAt(i)) in the below code to check for a double space? I am relatively new to Java.

String variable : text

character position: i

    import java.util.Scanner;
    public class Workbook {
    public static void main(String[] args) {
        String w;
        int Count = 0;
        Scanner V1 = new Scanner(System.in);
        System.out.println("Enter word: ");
        w = V1.nextLine();
        for(int i = 0; i<w.length()-1; i++){
                if (w.charAt(i) == ' ' && w.charAt(i + 1) != ' ') {
                    Count++;
                }

        }
        System.out.println("Number of words: " + (Count+1));
    }
}

Example problem would be that if I have an example string "Hello nice to meet you !" would output 6 words - which is correct

but " Hello nice to meet you! " outputs 6 words instead of 5.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
is43g
  • 1
  • 1

4 Answers4

1

The spaces before and after the sentence are causing the issue. Also, you can directly count the words as below:

String sentence = "Hello nice to meet you! ";
String[] wordSplit = (sentence.trim()).split(" ");
int count = wordSplit.length;
Tom
  • 16,842
  • 17
  • 45
  • 54
0

Split the words into a String array, separating on one or more whitespace characters after trimming the original word

String word = " Hello nice to meet you! ";

String[] words = word.trim().split( "\\s{1,}" ); // split all the words (after trimming)  into an array, seperating on one or more white space characters
System.out.println( words.length ); // output 5
RobOhRob
  • 585
  • 7
  • 17
  • As you can see in the comments under the question, this solution is already provided in another question, making this question a duplicate. So please don't repeat such answers, but flag this question as being a duplicate instead. – Tom Oct 02 '19 at 16:28
  • It wasn't a complete answer Tom.... The linked answer was to a question on `how to split a String on spaces`... it also did not provide the needed trimming – RobOhRob Oct 02 '19 at 16:30
  • Very subtle differences in the question – RobOhRob Oct 02 '19 at 16:31
  • The question don't matter, the answer do. And regarding trim:[How to split a String by space](//stackoverflow.com/a/30220543) – Tom Oct 02 '19 at 16:32
  • `The question don't matter`... I kinda disagree with you there bud – RobOhRob Oct 02 '19 at 16:32
  • Feel free to do so, the rules one duplicates are clear and disagreeing with you. – Tom Oct 02 '19 at 16:33
0

You should remove the trailing and starting space using trim. Otherwise count will increase by one even though there is no word between the first space and next non space character.

Sujay Mohan
  • 933
  • 7
  • 14
0

maybe you could use StringTokenizer

        String input = " Hello nice to meet you!";
        StringTokenizer stringTokenizer = new StringTokenizer(input);
        System.out.println(stringTokenizer.countTokens());

        while (stringTokenizer.hasMoreTokens()) {
            System.out.println(stringTokenizer.nextToken());
        }

Ludov Dmitrii
  • 425
  • 5
  • 9