【问题标题】:how to count the number of letters in an array java如何计算数组中的字母个数java
【发布时间】:2016-11-17 19:54:45
【问题描述】:

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

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);
                    }





            }
}

如果我输入“end tr” 输出为“12312” 但我想要的是“3和2”

我已经尝试了很多,但仍然无事可做...... 你能帮帮我吗?

【问题讨论】:

  • // TODO Auto-generated method stub 似乎为您生成了 IDE。在 IDE 中尝试调试器
  • 但是我是java新手..我不知道如何在IDE中尝试调试器..
  • 您正在循环内打印。把System.out.print(count);下面两行就行了

标签: java


【解决方案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++;
}

【讨论】:

  • 但是计数不能放在内部循环之外,因为我在内部循环中创建了计数...
  • 我明白了!太棒了 !谢谢!
【解决方案2】:
int countedLength = 0;

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

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

或者,如果您想计算每个字母的唯一性,请在此 for like 中创建一个新的 for

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

【讨论】:

    【解决方案3】:

    您正在使用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.
            }
        }
    }
    

    【讨论】:

    • 但是如果用户的输入是“as3”
    • 输出是 3.. 但我希望它是 2,因为它只有 2 个字母
    【解决方案4】:

    这应该可以解决问题,您在循环中打印您的计数,这就是它计数的原因。如果将初始计数值设置在它之外,则可以在循环完成后打印它,然后在下一个单词开始之前将其设置回 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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多