如何计算数组java中的字母数

时间:2016-11-17 11:14:15

标签: java

我有一个关于如何计算数组中字母数的作业。 这是我的代码:

import java.util.Scanner;

public class task1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub




            //Create a scanner object 
            Scanner input = new Scanner(System.in);

            //Prompt user's input
            System.out.println("Enter strings (use a space to separate them; hit enter to finish) : ");

            String str = input.nextLine();

            // use split to divide the string into different words cutting by " "
            String [] wordsArray = str.trim().split(" ");

            System.out.println("The length of string is " + wordsArray.length);




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


                char [] eachLetterinArray =  wordsArray[i].toCharArray();


                    for(int j = 0,count = 0 ; j < eachLetterinArray.length; j++){

                        if(    (eachLetterinArray[j]+'a'-97 >=65 && eachLetterinArray[j]+'a'-97 <=90 )
                            || (eachLetterinArray[j]+'a'-97 >=97 && eachLetterinArray[j]+'a'-97 <=122 )  ){

                            count++;

                        }

                        System.out.print(count);
                    }





            }
}

如果我进入&#34;结束tr&#34; 输出是&#34; 12312&#34; 但我想要的是&#34; 3和2&#34;

我已经尝试了很多但仍然无关于此... 你能救我吗?

4 个答案:

答案 0 :(得分:1)

您想要为每个单词打印count,但是您要为每个字符打印。只需在内循环之外打印count变量。

for (int i = 0; i < wordsArray.length; i++) {
    char[] eachLetterinArray = wordsArray[i].toCharArray();
    int count = 0;
    for (int j = 0; j < eachLetterinArray.length; j++) {
        if ((eachLetterinArray[j] + 'a' - 97 >= 65 && eachLetterinArray[j] + 'a' - 97 <= 90)
                || (eachLetterinArray[j] + 'a' - 97 >= 97 && eachLetterinArray[j] + 'a' - 97 <= 122)) {

            count++;
        }                
    }
    System.out.println(count);
}

<强>改进:

除了一点点复杂的条件,你也可以这样做:

if (Character.isLetter(eachLetterinArray[j])) {
    count++;
}

答案 1 :(得分:0)

您正在使用space分割单词并计算字母数。所以在字符串上使用split()

split()采用一个字符串来分割单词。在你的情况下,它是空间。

它将拆分的字符串作为数组返回。只需遍历字符串并在字符串上使用length()即可获得单词中存在的字符数。

public class Main
{
    public static void main(String[] args)
    {
        int count;
        String s = "This is your string for example";
        String[] split = s.split(" ");
        for(String str: split)
        {
            char[] ch = str.toCharArray();   // convert string to char array
            count = 0;                       // reset count for every new word/string
            for(char c: ch)                  // iterate over all the characters
            {
                if(Character.isLetter(c))    // Returns true if the character is a Letter
                {
                    count++;                 // increase the count to represent no. of letters
                }
        }
        System.out.print(count + " ");       // print the no.of characters that are letters in a word/string.
        }
    }
}

答案 2 :(得分:0)

int countedLength = 0;

for(String string: arrayList) {
countedLength += string.length();
}

//Your count of Number of Letters in Array
System.out.pritnln(countedLength);

或者,如果您想要将每个字母统计为唯一,请在此处执行新的操作,例如

if(letter.equals("a")) {
letterVariableCountA++;
}

答案 3 :(得分:0)

这应该可以解决问题,你在循环中打印你的计数,这就是它计数的原因。如果在其外部设置初始计数值,则可以在循环完成后打印它,然后在下一个单词开始之前将其设置为0.

public static void main(String[] args) {
    String str = "test1 phrase";
    int count = 0;

    // use split to divide the string into different words cutting by " "
    String[] wordsArray = str.trim().split(" ");

    System.out.println("The length of string is " + wordsArray.length);

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


        char[] eachLetterinArray = wordsArray[i].toCharArray();


        for (int j = 0; j < eachLetterinArray.length; j++) {

            if ((eachLetterinArray[j] + 'a' - 97 >= 65 && eachLetterinArray[j] + 'a' - 97 <= 90)
                    || (eachLetterinArray[j] + 'a' - 97 >= 97 && eachLetterinArray[j] + 'a' - 97 <= 122)) {

                count++;
            }
        }
        System.out.print(count + "\n");
        count = 0;
    }
}

通过上面的例子我得到了

的输出
The length of string is 2
4
6