尝试创建一个方法(和测试)来计算字符串中的所有数字、字母和字符,并且

Trying to create a method (and test) that counts all numbers,letters,and chars in a string and

...并将它们放入大小为 4 的数组中。

index 0 = 多少个大写字母

index 1 = 多少个小写字母

index 2 = 多少个数字

索引 3 = 其他

这是我的代码:

   package code;

public class PotentialQ {

    public int[] countAll(String input){

        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int [] fin = new int [4];

        for(int i = 0; i < input.length(); i++){

            char ch = input.charAt(i);

            if(Character.isLetter(ch)){
                if(Character.isUpperCase(ch)){
                    count1++;
                }
                else
                    count2++;
            }
            else if(Character.isDigit(ch)){
                count3++;
            }
            else
                count4++;
        }
        fin[0] = count1;
        fin[1] = count2;
        fin[2] = count3;
        fin[3] = count4;

        return fin;

    }

}

这是我的测试:

 package tests;

import org.junit.Test;
import static org.junit.Assert.assertTrue;

public class PotentialQTests {

        int [] arr = {1,3,3,1};

        @Test public void test1() {testCode("Baaa123!", arr);}

        private void testCode(String input, int [] expected) {
            code.PotentialQ pq = new code.PotentialQ();
            int [] actual = pq.countAll(input);
            assertTrue("It was " +actual, expected == actual);
        }
    }

我的错误是一堆字母和数字。 我的代码有什么问题???

这一行:

assertTrue("It was " +actual, expected == actual);

您正在对原始数组调用 ==,我建议将其更新为

assertArrayEquals(expected, actual);

描述 "It was " +actual 不会告诉你太多 - 只是数组的引用。要查看您可以执行的内容

assertArrayEquals("It was " + Arrays.toString(actual), expected, actual);