Counting words in a string

Using the split method but getting an error

Page 1 of 1

7 Replies - 5289 Views - Last Post: 17 November 2010 - 06:42 PM Rate Topic: -----

#1 Guest_Sebastian*


Reputation:

Counting words in a string

Posted 17 November 2010 - 06:12 PM

Hello, first time here :)

Anyway I'm trying to add an extension to my program in my class that will count how many words there are in a given string. I've done about an hour of research and I found out about the split command. I don't know how to fully use it but I think I got it down correctly in my code. But I am having another problem.

I am getting an outofbounds error on the line where the if statement starts:
AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5
The 5 at the end is uncoincidentally the number of words that I enter in the sentence :(

I tried to get it so that when I push a button it will tell me the # of words. Oh and should I be displaying "count" as the # of words?


if(e.getSource()==b4)
    {
      s6=t7.getText();
      sv=s6.length();
      String []splitter=s6.split(" ");
      int count=0;
      for(int i=0;i<sv;i++)
      {
      if(splitter[i].equals(" "))
      {}
      else
      {count++;}
      }
    }



I tried but we just started string manipulation and haven't actually learned the split command. Thanks for any assistance!

Is This A Good Question/Topic? 0

Replies To: Counting words in a string

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: Counting words in a string

Posted 17 November 2010 - 06:15 PM

The split() method will remove all whitespace from your String, so you don't need to test for it. Just get the length of the returned array from split(), and that is how many words are in the String. :)
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Sebastian*


Reputation:

Re: Counting words in a string

Posted 17 November 2010 - 06:22 PM

View Postmacosxnerd101, on 17 November 2010 - 05:15 PM, said:

The split() method will remove all whitespace from your String, so you don't need to test for it. Just get the length of the returned array from split(), and that is how many words are in the String. :)


So I basically can I just count the number of times the array was repeated in the "for" loop and use that? But why am I getting that error?
Was This Post Helpful? 0

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: Counting words in a string

Posted 17 November 2010 - 06:23 PM

You don't even need to use a for loop. Just access the array's length variable.
//numWords = 4
int numWords = "This is a test".split(" ").length; 


Was This Post Helpful? 0
  • +
  • -

#5 Guest_Sebastian*


Reputation:

Re: Counting words in a string

Posted 17 November 2010 - 06:29 PM

View Postmacosxnerd101, on 17 November 2010 - 05:23 PM, said:

You don't even need to use a for loop. Just access the array's length variable.
//numWords = 4
int numWords = "This is a test".split(" ").length; 



Ok sorry if I don't follow, but:
-they enter their sentence into the GUI, I convert it to a string with .getText
-how do I split this?

Where you put "This is a test" can I just put the string in there?
Was This Post Helpful? 0

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: Counting words in a string

Posted 17 November 2010 - 06:32 PM

Once the text is entered, you do need to get the text. Then invoke split() off the returned String, as I did. This method returns a String[]. From there, access the length variable of the array as I did in my example.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Sebastian*


Reputation:

Re: Counting words in a string

Posted 17 November 2010 - 06:38 PM

View Postmacosxnerd101, on 17 November 2010 - 05:32 PM, said:

Once the text is entered, you do need to get the text. Then invoke split() off the returned String, as I did. This method returns a String[]. From there, access the length variable of the array as I did in my example.


Thank you so much! I wish I could repay you! Thank you!
Was This Post Helpful? 0

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: Counting words in a string

Posted 17 November 2010 - 06:42 PM

Not a problem. Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1