The Java Code for Counting the Number of Words in an Array

Written by sue smith Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
The Java Code for Counting the Number of Words in an Array
Java string arrays often store text from sources such as user input. (Thinkstock Images/Comstock/Getty Images)

Java applications often use arrays to store data items such as text strings. If a program is storing strings within an array, it may be helpful to calculate the total number of words contained. If each array element contains only a single word, then the number of words is simply the length of the array. However, if the array elements may contain more than one word, an algorithm will be necessary to work out the total.

Other People Are Reading

Array Creation

Java programs can create string arrays directly or by reading text in from an external source, such as a file or database. The following example syntax demonstrates creating an array with a few string variables in it:

String[] someWords = {"apple", "banana orange", "grape melon cherry"};

This code creates and instantiates the array at the same time, filling it with a variable number of words at each index position. The technique of counting the words in an array is the same for any text strings.

Traversal

To traverse an array, Java programs often use "loops," with each loop iteration focusing on a single array element, moving along until the entire structure has been explored. The following sample code demonstrates iterating through the array:

for(int i=0; i<someWords.length; i++) {

System.out.println("Index "+i+": "+some Words[i]);

}

This trivial example writes out the text value at each position along with the integer index representing the element position. The "for" loop often appears within Java programs featuring array structures.

Array Elements

In cases where an array contains only a single word per element, you can calculate the number of words held within the array by finding out the array length, which is an integer value representing how many element positions the array contains. For example, the following alternative array contains only single words:

String[] someWords = {"apple", "banana", "grape"};

In this case, the number of words is equivalent to the number of array positions, as in the following code:

int numElements = someWords.length;

String Split

If a string array in a Java program contains multiple words within each string element, the code will need to count how many words are listed within each element, adding these together to arrive at the total figure. To calculate how many words appear within a text string, the split method can be helpful. The string split method divides the string on a given character set represented using a regular expression. For example, the following code splits a string on any characters that are not alphanumeric:

String sentences = "Here are some words. Here are some more.";

String[] words = sentences.split("[^a-zA-Z0-9]+");

Multiple Words Per Element

To calculate the number of words within a Java string array in which each element may contain multiple words, program logic must calculate the total for each element, adding to a total for the array as a whole. The following example code demonstrates the technique:

String[] someWords = {"apple", "banana orange", "grape melon cherry"};

int totalWords = 0;

for(int i=0; i<someWords.length; i++){

totalWords += someWords[i].split("[^a-zA-Z0-9]+").length;

}

The total counter is created before the loop begins, so that code inside the loop can add to it, and it can be accessed after the loop has finished executing.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.