Find Number of Words in a String

i want to calculate number of words in a string say-"you are write

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 19211 Views - Last Post: 24 July 2010 - 09:43 AM Rate Topic: -----

#1 rakesh0  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 11
  • Joined: 23-July 10

Find Number of Words in a String

Posted 23 July 2010 - 04:56 AM

m new to programming i want to find no. of words in any string so can anybody have any logic if yas then tell me,,,,
Is This A Good Question/Topic? 0
  • +

Replies To: Find Number of Words in a String

#2 rakesh0  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 11
  • Joined: 23-July 10

Re: Find Number of Words in a String

Posted 23 July 2010 - 05:14 AM

please give me some code for that purpose,,,,,,
Was This Post Helpful? 0
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Find Number of Words in a String

Posted 23 July 2010 - 05:27 AM

Number of words...maybe try using the split() method and split according to whitespaces?

And count the length of the array returned by that method:

String[] words = sentence.split(" ");
//words.length is your # of words


Was This Post Helpful? 0
  • +
  • -

#4 rakesh0  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 11
  • Joined: 23-July 10

Re: Find Number of Words in a String

Posted 23 July 2010 - 05:35 AM

i still not getting the output can you send me the code,,,,??? piz m new to programming
Was This Post Helpful? 0
  • +
  • -

#5 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Find Number of Words in a String

Posted 23 July 2010 - 05:49 AM

We will not write code for you, feel free to provide some code you have done to show us that you have put effort in.

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#6 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Find Number of Words in a String

Posted 23 July 2010 - 05:52 AM

View Postrakesh0, on 23 July 2010 - 08:35 AM, said:

i still not getting the output can you send me the code,,,,??? piz m new to programming



Which is exactly why we shouldn't send you the code. If you just always have code handed to you then you will not learn. Post what you've done. You said you're not getting the desired output so you must have done something.

Let's see it!
Was This Post Helpful? 2
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:02 AM

Title renamed to be more descriptive. Please respect rule #4 of this forum. "Java" is not a helpful title in a forum dedicated to Java Programming.

Also, I want to reiterate rule #1. We will be happy to help you debug code you wrote, but we will not give you the code.
Was This Post Helpful? 0
  • +
  • -

#8 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:04 AM

Here's a hint. Do what bcranger said, and use a for loop to iterate through the "word" array, use equals() method to find which word is equal to the word you want to find, and increase a "count" variable when the word is equal. I hope this is clear enough to give you a headstart. Well, actually it's clear enough to let you write the whole app. XD
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,167
  • Joined: 11-December 07

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:13 AM

I think he just wants the number of words. If you provide a string to that snippet bcranger gave you, it works out the number of words for you. All you need then is a System.out.println(???????) to display them.
Was This Post Helpful? 0
  • +
  • -

#10 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:15 AM

View Postcfoley, on 23 July 2010 - 12:13 PM, said:

All you need then is a System.out.println(???????) to display them.

Leave that for him/her to do atleast ;)
Was This Post Helpful? 0
  • +
  • -

#11 rakesh0  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 11
  • Joined: 23-July 10

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:40 AM

actually what m trying to do is..m trying to count the no. of words usig that code but unable to do,,,,,help me
class words
{
     public static void main (String args[])
        {
                 String name="welcome to java";
                 int l=0;
                 int i;
                 int j;
                 for(i=0;i<name.length;i++)
                 {
                      for(j=0;j<=name[i].length()-1;i++)
                                   {
                                     if(name[i].charAt(i)==' ')
                                    {
                                        l++;
                                    }
     }
}
System.out.println(+l);
}}


Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#12 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:43 AM

Well why not try what bcranger said? You don't need any loops for this.

This post has been edited by eZACKe: 23 July 2010 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:44 AM

rakesh0, please use code tags, like so: :code:. Also, please properly format your code so we can read it easier. Why don't you just use bcranger's solution?
Was This Post Helpful? 0
  • +
  • -

#14 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:45 AM

macosxnerd101, could you merge this thread?
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Find Number of Words in a String

Posted 23 July 2010 - 06:47 AM

They are different topics, as that one is trying to find the number of occurrences of a character in a String. I'm going to keep the topics as they are for now.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3