2016-11-17 65 views
-3

我有关于如何计算在阵列中的字母数分配的字母数。 这里是我的代码:如何计算阵列中的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); 
        } 





      } 
} 

,如果我输入“结束TR” 输出为“12312” ,但我要的是“3 2”

我已经尝试了很多,仍然有对此无关...... 你能帮助我吗?

+0

输出'// TODO自动生成方法stub'似乎IDE为你生成。尝试调试器IDE – NewUser

+0

但我在Java中新..我不知道如何尝试在IDE .. –

+0

http://stackoverflow.com/questions/18977397/debug-java-program-step-by-step调试-in-eclipse – NewUser

回答

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++; 
} 
+0

,但数量不能在内环之外,因为我在内部循环创建数放... –

+0

看到我编辑的代码。这将工作。 – Shahid

+0

我懂了!太棒了 !谢谢 ! –

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. 
     } 
    } 
} 
+0

但如果用户输入的是什么“AS3” –

+1

输出为3 ..但我希望它是2,因为它只有2个字母 –

+0

@ Calvin.Xie编辑的代码。 – SkrewEverything

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++; 
} 
0

这应该做的伎俩,您在打印循环中的计数其这就是为什么它在计数。如果设置的是外面的inital计数值,然后你可以打印一次循环完成,然后将其设置回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