2013-02-25 149 views
3

我只有循環1次是什麼問題的for循環方法有麻煩?在數組中完全沒有問題,它能夠打印我想要的值。For循環僅循環1次JAVA?

這裏是我的代碼:", "

public static void main(String[] args){ 

     String s = "Apple0, Apple1, Apple2, Apple3, Apple4"; 
     String[] word = s.split(","); 
     StringBuffer str = new StringBuffer(); 
     Integer total = 0; 

     for (int y = 0; y < word.length; y++){ 
      if(word[y].toString().equals("Apple2")){ 
       total++; 
       //str.append(word[y].toString()); 
      }else if(word[y].toString().equals("Apple3")){ 
       total++; 
       //str.append(word[y].toString()); 
      }else if(word[y].toString().equals("Apple4")){ 
       total++; 
       //str.append(word[y].toString()); 
      } 
      else if(word[y].toString().equals("Apple1")){ 
       total++; 
       //str.append(word[y].toString()); 
      } 


    } 
     System.out.println(word[0] + word[1] + word[2] + word[3] + word[4] + word.length); 
     System.out.println(str + "hihi" + total); 

} 
+3

爲什麼在一個已經是字符串的對象上調用'.toString()'? – 2013-02-25 03:24:32

+0

我的錯誤,但我刪除它的循環仍然循環1次?任何解決方案 – cchua 2013-02-25 03:26:22

回答

3

嘗試分裂(含空格)

String[] word = s.split(", "); 

沒有分裂word[1]這個空間看起來像" Apple1"代替"Apple1"


其他選項是c排除word[y].trim().equals("Apple2")擺脫額外的空間,但我會說包括它在分裂更好。如果您不確定逗號附近有多少空格,您可以按照這種方式拆分split("\\s*,\\s*")以包含逗號周圍的所有空格。


而且馬特球在他的評論中指出的,你不需要調用toString()word[y],因爲它已經是字符串。

+1

+1 - 事實上,for循環沒有任何問題。它是循環體中的代碼被破壞的......'因爲它沒有考慮到單詞中的前導/尾隨空格。 – 2013-02-25 03:28:37

+0

確實。添加'System.out.println(「用y =」+ y)迭代;'在'for'後面立即說明循環迭代,除非'word [y]'有一個期望值,否則什麼也不做。 – 2013-02-25 03:32:20

1

你被「,」分割,但你的字符串包含「,」。 您可以更改s.split(",");s.split(", ");

或修剪分割的結果是這樣的:

public static void main(String[] args) { 

     String s = "Apple0, Apple1, Apple2, Apple3, Apple4"; 
     String[] word = s.split(","); 
     StringBuffer str = new StringBuffer(); 
     Integer total = 0; 
     for (int y = 0; y < word.length; y++) { 
      if (word[y].trim().equals("Apple2")) { 
       total++; 
       // str.append(word[y].toString()); 
      } else if (word[y].trim().equals("Apple3")) { 
       total++; 
       // str.append(word[y].toString()); 
      } else if (word[y].trim().equals("Apple4")) { 
       total++; 
       // str.append(word[y].toString()); 
      } else if (word[y].trim().equals("Apple1")) { 
       total++; 
       // str.append(word[y].toString()); 
      } 

     } 
     System.out.println(word[0] + word[1] + word[2] + word[3] + word[4] 
       + word.length); 
     System.out.println(str + "hihi" + total); 

    } 
2

你忽略分裂過程中的空間。 String [] word = s.split(「,」);

6

其他人已經確定了你的問題的原因。但是,他們所提出的解決方案過於具體......而且很脆弱。 (與split("\\s*,\\s*")分裂更好,但它不會在整個字符串的開始/結尾處理空白)。

我建議您繼續使用split(","),但在測試之前修剪單詞;例如

for (int y = 0; y < word.length; y++) { 
     String trimmed = word[y].trim(); 
     if (trimmed.equals("Apple2")) { 
      total++; 
      //str.append(trimmed.toString()); 
     } else if (trimmed.equals("Apple3")) { 
      // etcetera 

或者更好的是:

String[] words = s.split(","); 
    for (String word : words) { 
     String trimmed = word.trim(); 
     if (trimmed.equals("Apple2")) { 
      total++; 
      //str.append(trimmed.toString()); 
     } else if (trimmed.equals("Apple3")) { 
      // etcetera 

這將使你的代碼的工作,不論周圍的逗號和字符串的開頭和結尾的空格字符。健壯性是好的,特別是如果它的成本幾乎沒有實施。

最後,您甚至可以用Java 7 String switch語句替換if/else if/...。

0

你的代碼沒有問題,但問題在於你給變量的字符串。

String s = "Apple0, Apple1, Apple2, Apple3, Apple4"; 

這裏的字符串在逗號後面包含它們之間的空格。因此,當你拆分你的字符串時,它會像

word[0]= "Apple0" 
word[1]= " Apple1" 
word[2]= " Apple2" 
word[3]= " Apple3" 

等等。

所以,當你比較喜歡

字[Y] .equals( 「Apple1」),因爲 「Apple1」 和 「Apple1」 是兩個不同的字符串返回false。因此,初始化您的字符串像這樣

String s = "Apple0,Apple1,Apple2,Apple3,Apple4"; // without white spaces 

它會正常工作。或者你可以在你現有的代碼中使用裝飾方法在不改變字符串就像

word[y].trim().equals("Apple1") //It will trim all the leading and trailing white spaces. 

希望這有助於。