簡體   English   中英

對於循環僅循環1次JAVA?

[英]For loop only loop 1 time JAVA?

我有一個for循環方法的問題,只有循環1次是什么問題? 在數組中沒有任何問題,它能夠打印我想要的值。

這是我的代碼:

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

}

其他人已經找到了問題的原因。 但是,他們提出的解決方案太具體了......而且很脆弱。 (拆分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 / ... stuff。

嘗試拆分", " (帶空格)

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

沒有那個空格的分word[1]看起來像" Apple1"而不是"Apple1"


其他選項是調用word[y].trim().equals("Apple2")來擺脫那個額外的空間,但我會說將它包含在分裂中更好。 如果你不確定逗號附近有多少個空格,你可以用這種方式split("\\\\s*,\\\\s*")以包含逗號周圍的所有空格。


同樣,Matt Ball在他的評論中指出,你不需要在word[y]上調用toString() ,因為它已經是String。

你在分裂時忽略了空間。 String [] word = s.split(“,”);

你用“,”分開,但你的字符串包含“,”。 你可以改變s.split(","); s.split(", ");

或者像這樣修剪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);

    }

您的代碼沒有任何問題,但問題在於您給變量的String。

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

這里的字符串在逗號后面包含空格。 因此,當你拆分你的字符串時,它會分裂

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

等等。

所以當你比較時

word [y] .equals(“Apple1”)它返回false,因為“Apple1”和“Apple1”是兩個不同的字符串。 所以像這樣初始化你的字符串

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

它會工作正常。 或者您可以在現有代碼中使用trim方法而不更改String之類的

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM