count number of words in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kishore314
    New Member
    • Jan 2007
    • 4

    count number of words in a string

    Hi,
    Just a small excerise for me.
    A program that counts number of words in a string.

    The program which i have written now is -
    Code:
    import java.io.*;
    class countword 
    {
    	private static BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
    
    	public static void main(String arg[]) throws IOException  {
    	System.out.println("Enter a string: ");
    	String input = stdin.readLine();
    
    	String[] arr = input.split(" ");
    	int length = arr.length;
    	System.out.println ("Lenght of string: "+input+" is:"+length);
       }
    }
    But this counts even extra blank spaces. Like this string "I am new to JAVA",
    or " I am new to java" should count only as 5 words, but the result shows more than that.

    Any suggestions to change the logic?


    Cheers,

    Kishore
    Last edited by Nepomuk; Nov 22 '10, 02:51 PM. Reason: Please use [code] tags.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by kishore314
    Hi,
    Just a small excerise for me.
    A program that counts number of words in a string.

    The program which i have written now is -

    import java.io.*;
    class countword
    {
    private static BufferedReader stdin = new BufferedReader( new InputStreamRead er( System.in ));

    public static void main(String arg[]) throws IOException {
    System.out.prin tln("Enter a string: ");
    String input = stdin.readLine( );

    String[] arr = input.split(" ");
    int length = arr.length;
    System.out.prin tln ("Lenght of string: "+input+" is:"+length);
    }
    }


    But this counts even extra blank spaces. Like this string "I am new to JAVA",
    or " I am new to java" should count only as 5 words, but the result shows more than that.

    Any suggestions to change the logic?


    Cheers,

    Kishore
    From
    Code:
     String[] arr = input.split(" ");
    then you do
    Code:
     int count = 0; 
    for(int i = 0; i <arr.length;i++) {
    if(arr[i].equals(" ")) {
    }
    else {
    	count++;
    }
    }
    and
    Code:
     int length = count;

    Comment

    • kishore314
      New Member
      • Jan 2007
      • 4

      #3
      hey,
      that was a very quick reply!! Really unexpected and great ;-)

      it works fine.

      Thanks a lot!

      Cheers,
      Kishore

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by kishore314
        hey,
        that was a very quick reply!! Really unexpected and great ;-)

        it works fine.

        Thanks a lot!

        Cheers,
        Kishore
        Anytime Kishore. Just remember to use code tags next time you post your code either in helping others or in asking more questions

        Comment

        • kishore314
          New Member
          • Jan 2007
          • 4

          #5
          Originally posted by r035198x
          Anytime Kishore. Just remember to use code tags next time you post your code either in helping others or in asking more questions
          I am a newbie to this forum, but i get what u meant :-)

          hey, is there any other method WITHOUT USING SPLIT() for this program?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by kishore314
            I am a newbie to this forum, but i get what u meant :-)

            hey, is there any other method WITHOUT USING SPLIT() for this program?
            Combination of split and a regular expression is the best approach. The tokenizer route is becoming archaic

            Comment

            • kishore314
              New Member
              • Jan 2007
              • 4

              #7
              Originally posted by r035198x
              Combination of split and a regular expression is the best approach. The tokenizer route is becoming archaic
              No clue how to go about it...HELP!!!

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by kishore314
                No clue how to go about it...HELP!!!
                You'd need to go through a regular expression tutorial first. One is found here

                Comment

                • Glenn Setliff

                  #9
                  Looks like the solution is instead to use input.split("\\ s+") to get the count instead of input.split(" ") and then just output the length.

                  Comment

                  • Parth Gallen
                    New Member
                    • Nov 2010
                    • 3

                    #10
                    "one&two#three| four" will return as 4 words.
                    "I'm here" 3 words and so on.
                    Just didn't think " " is enough to separate words.

                    Code:
                    public static int countWords(String s) {
                        int counter = 0;
                        boolean word = false;
                        int endOfLine = s.length()-1;
                    
                        for (int i = 0; i < s.length(); i++) {
                          //if the char is letter, word = true.
                          if (Character.isLetter(s.charAt(i)) == true && i != endOfLine) {
                            word = true;
                          //if char isnt letter and there have been letters before (word == true), counter goes up.
                          } else if (Character.isLetter(s.charAt(i)) == false && word == true) {
                            counter++;
                            word = false;
                          //last word of String, if it doesnt end with nonLetter it wouldnt count without this.
                          } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
                            counter++;
                          }
                        }
                        return counter;
                      }

                    Comment

                    • eeree
                      New Member
                      • Nov 2011
                      • 1

                      #11
                      Try splitting with Regular Expressions instead of using " " as delimiter:
                      Code:
                      String[] arr = input.split("\\s+");
                           int length = arr.length;
                      That code will split String on any space character (space \n \r or \t) (if there is more characters in a row it will split only once).

                      Comment

                      • 123jyotsna
                        New Member
                        • Jan 2012
                        • 1

                        #12
                        Could use the following code:

                        import java.io.*;

                        public class Countstringword s {

                        private static BufferedReader stdin = new BufferedReader( new InputStreamRead er( System.in ));

                        public static void main(String arg[]) throws IOException {
                        System.out.prin tln("Enter a string: ");
                        String input = stdin.readLine( );
                        int i,l = 0;
                        String[] arr = input.split(" ");
                        int length = arr.length;
                        for(i=0;i<lengt h;i++){
                        System.out.prin tln(arr[i]);
                        if(!"".equals(a rr[i])){l++;}

                        }
                        System.out.prin tln ("Lenght of string: "+input+" is:"+l);
                        } }

                        Comment

                        • Rica Vida
                          New Member
                          • Feb 2012
                          • 1

                          #13
                          can u explain that.,please.,i need explanation.,tn x

                          Comment

                          Working...