3

I have a trouble with the for loop method that only loop 1 times whats is the problem? In the array was no problem at all, it able to print the value I want to.

here is my code:

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

}
4

5 回答 5

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 / ... 东西。

于 2013-02-25T03:36:15.237 回答
3

Try splitting on ", " (with space)

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

without that space in split word[1] would look like " Apple1" instead "Apple1"


Other option would be calling word[y].trim().equals("Apple2") to get rid of that additional space, but I would say including it in split is better. If you aren't sure how many white-spaces can be near comma you can split this way split("\\s*,\\s*") to include all white-spaces around comma.


Also as Matt Ball pointed in his comment you don't need to call toString() on word[y] since it is already String.

于 2013-02-25T03:26:11.753 回答
2

您在拆分期间忽略了空间。String[] word = s.split(", ");

于 2013-02-25T03:28:01.377 回答
1

You'are split by "," but your String contains ", ". You can change the s.split(","); to s.split(", ");

Or trim the split's result like this :

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

    }
于 2013-02-25T03:27:25.597 回答
0

您的代码没有任何问题,但问题在于您提供给变量的字符串。

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.

希望这可以帮助。

于 2013-02-25T03:51:53.123 回答