Results 1 to 9 of 9
  1. #1
    andy5605 is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default [SOLVED] How to count the number of words in a string

    Hey there just found these forums and this is my first post, so hey just looking for a bit of help.
    I have to write a program witch takes in a string and outputs the number of letters and words in it, it is wreking my head big time! I can could the letters no problem but the words I've been trying to do it for days now and I just can't seem to get it, would be really thankful to any help.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Check out the split() method in the String API

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by emceenugget View Post
    Check out the split() method in the String API
    ... and I was just going to mention that one... Getting slow in my old age.

  4. #4
    swathi palla is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default try it

    you can use string buffers concept here..

    for example..

    class length
    {
    public static void main(String args[])
    {
    StringBuffer sb = new StringBuffer("feelfree");

    System.out.println("Length="+sb.length());
    }
    }
    output:
    length=8

  5. #5
    andy5605 is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the replys, I'm very new to java and I'm finding it hard I have tryed to do the stuff you said but it just aint working, I've been doing this piece of code for about a hour now its probaly horriably wrong :P anyway it all compiles but i get 1 error, class, interface, or enum expected and it shows me that the error is on the last line, anyway heres the code and thanks for replying guys :)


    class wordcount


    {


    private static long wordcount(String line)

    {




    long numChar = 0;
    long numLine=0;
    long numWords = 0;
    // String line;

    System.out.println("Enter a String ") ;
    line = EasyIn.getString() ;




    // long numWords = 0;
    int index = 0;
    boolean prevWhiteSpace = true;
    while(index < line.length()){
    char c = line.charAt(index++);
    boolean currWhiteSpace = Character.isWhitespace(c);
    if(prevWhiteSpace && !currWhiteSpace){
    numWords++;

    prevWhiteSpace = currWhiteSpace;







    }
    }

    }

    }
    }

  6. #6
    andy5605 is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Right I've scraped that piece of code and I'm going with this one I ike it more than the other one here it is, only have one error and that is: cannot find symbol method nextLine()


    Code:



    class Wordcount
    {

    public static void main (String args[])

    {

    System.out.println("Enter a String ");


    String a = "";
    String userInput;

    a = userInput.nextLine();

    int textLength = a.length();
    int numberOfWords = 0;
    int countChar = 0;
    boolean isWord = false;

    for (int i=0;i<textLength;i++)
    {
    if (a.charAt(i) == ' ' || a.charAt(i) == '.'|| a.charAt(i) == '?' || a.charAt(i) == '!')
    {
    numberOfWords++;
    isWord = true;
    }

    else
    {
    countChar++;

    }



    }
    System.out.println("Word " + numberOfWords);
    }
    }

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    look at this line:
    String userInput;
    a = userInput.nextLine();

    userInput is suppose to be Scanner, not String.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    andy5605 is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Cheers that fixed it :)

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    if your prob is solved, please mark it as solved by using the thread tool bar at the very top.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Replies: 5
    Last Post: 11-22-2008, 01:34 PM
  2. Replies: 4
    Last Post: 07-07-2008, 01:32 PM
  3. Replies: 1
    Last Post: 07-02-2008, 01:58 PM
  4. Replies: 2
    Last Post: 05-04-2008, 10:52 PM
  5. Analyze a string of words
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •