user2971033
user2971033

Reputation:

Calculate number of letters in string array?

I am trying to calculate the total number of letters within the array? I was attempting to change the array to string, then calculate total string length (code below) but cant get it to work? (point of interest in Bold) Thanks to anyone in advance.

package practical5;

import java.util.Arrays;

public class Part1_9 {

public static void main(String[] args) {

    // declaring and populating array
    String quoteArray[] = { "\"Continuous", "effort", "not", "strength",
            "nor", "intelligence", "is", "the", "key", "to", "unlocking",
            "our", "potential.\"\n" };

    // for loop to print full array
    for (int counter = 0; counter < quoteArray.length; counter++) {
        System.out.print(quoteArray[counter] + " ");
    }// end of for loop

    // Printing array using Enhanced for/ for each loop (Different way to
    // print array)
    for (String element : quoteArray) {
        System.out.print(element + " ");
    }// end of enhanced for

    // line break
    System.out.println();

    // printing number of words in array
    System.out.println("Number of words in array: " + quoteArray.length);

    **// printing total number of letters in array**
    for (int counter = 0; counter < quoteArray.length; counter++) {
        String letters = new String(quoteArray[counter]);
    }

    // printing the smallest word

    // printing the biggest word

}// end of main

}// end of class

Upvotes: 1

Views: 18389

Answers (6)

ajb
ajb

Reputation: 31699

Computing the number of letters in a string will contain code that looks something like:

for (int i = 0; i < s.length(); i++)
    if (Character.isLetter(s.charAt(i)))
        // something

where s is the string. charAt returns the i'th character of the string (where the first character is charAt(0)), and Character.isLetter tests whether the character is a letter. I'll let you figure out how to use this and what you might want to use for s.

Upvotes: 2

Ievgen Derevianko
Ievgen Derevianko

Reputation: 11

for

printing total number of letters in array

look on java.lang.Character

Upvotes: 0

Anna Wygant
Anna Wygant

Reputation: 126

quoteArray.length will only give you the number of elements in the array (in your case the output would be 13).

To get the length of all the strings combined, create a length variable and add to it as you iterate through each array element. Use .length() to get the length of each element and add it to the total length:

int totalLength = 0;
for(String element : quoteArray){
    totalLength+=element.length();
}
System.out.println("The total length of the quote is: " + totalLength);

Upvotes: 0

user2971033
user2971033

Reputation:

Sorry I think i've Worked it out now: 1. Use an int (in this case called total) to keep a running total of letters in each element. 2.use arrayName[counter].length to get length of each individual element. 3. user counter++ to iterate through each each element untill the end of the array.

// printing total number of letters in array
        for (int counter = 0; counter < quoteArray.length; counter++) {
             total +=quoteArray[counter].length();
        }

            System.out.println("Total length of array is: " + total);

Upvotes: 1

user1050632
user1050632

Reputation: 668

This is rough code, not ran:

Take the array of strings

for each value in the array count += string length [i]

taht will count every character of each string in your array.

for finding the smallest string in the array, just keep track of each string length, and compare them, it's a basic search algorithm.

Upvotes: 0

user2864228
user2864228

Reputation:

int totalCount = 0;
for(String s : quoteArray) {
    totalCount += s.length();
}

Upvotes: 0

Related Questions