2016-11-17 61 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