字符串中的特殊字符[JAVA]

时间:2015-04-07 05:12:14

标签: java

伙计们,如果找到除"01xX \t"之外的其他字符(包括传递的String中的空格和\ t),则下面的方法将抛出异常。如果我有这个字符串"1 x \tX 00",该方法应该返回[1,X,X,X,X,X,X,X,0,0],但我只获得[1,X,X,0,0],其中'whitespace''\t'不会被包含在内。 'Whitespace''\n'也应返回'X'。请问可以帮助我吗?

    //Here's the test case that I'm failing
     @Test (timeout=3000) public void signal13(){
        String inp = "1 x \tX 00";
        List<Signal> expecteds = Signal.fromString(inp);
        assertEquals(expecteds, Arrays.asList(new Signal[]{Signal.HI, Signal.X, Signal.X, Signal.LO, Signal.LO}));
      }





    import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public enum Signal {

    HI, LO, X;


    public Signal invert()
    {
        if(this == HI)
            return LO;
        else if(this == LO)
            return HI;
        else if(this == X)
            return X;

        return this;
    }

    public static Signal fromString(char c)
    {

        if(c == '1')
            return HI;
        else if(c == '0')
            return LO;
        else if(c == 'X')
            return X;
        else if(c == 'x')
            return X;
        else
            throw new ExceptionLogicMalformedSignal(c, "Invalid character!");

    }

    public static List <Signal> fromString(String inps)
    {
        List<Signal> values = new ArrayList<Signal>();
        for(int i = 0; i < inps.length(); i++)
        {
            if(inps.charAt(i) == '1')
                values.add(HI);
            else if(inps.charAt(i) == '0')
                values.add(LO);
            else if(inps.charAt(i) == 'X')
                values.add(X);
            else if(inps.charAt(i) == 'x')
                values.add(X);
            else if(inps.charAt(i) == ' ')
                values.add(X);
            else if(inps.charAt(i) == '\t')
            {
                values.add(X);
                values.add(X);
            }
            else 
                throw new ExceptionLogicMalformedSignal(inps.charAt(0), "Invalid character!");
        }
        return values;

    }

    @Override
    public String toString()
    {
        if(this == HI)
            return "1";
        else if(this == LO)
            return "0";
        else if(this == X)
            return "X";

        return "Error here!";
    }
    public static String toString(List<Signal> sig)
    {
        String result = "";
        ArrayList<Signal> temp = new ArrayList<>();
        for(Signal x: sig)
        {
            temp.add(x);
        }

        for(int i = 0; i < temp.size(); i++)
        {
            if(temp.get(i) == HI)
                result += "1";
            else if(temp.get(i) == LO)
                result += "0";
            else if(temp.get(i) == X)
                result += "X";
        }

        return result;
    }
}

2 个答案:

答案 0 :(得分:3)

似乎断言不正确,它是:

 assertEquals(expecteds, Arrays.asList(new Signal[]{Signal.HI, Signal.X, Signal.X, Signal.LO, Signal.LO}));

虽然它应该是:

List<Signal> actual = Signal.fromString(inp);
List<Signal> expected = Arrays.asList(new Signal[]{Signal.HI, Signal.X, Signal.X,Signal.X,Signal.X,Signal.X,Signal.X,Signal.X, Signal.LO, Signal.LO});
 assertEquals(expected, actual);

因为预期结果为[1,X,X,X,X,X,X,X,0,0]

答案 1 :(得分:0)

这是您的代码的修改版本,可以根据需要运行...

private String fromString(String inps) throws Exception {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < inps.length(); i++) {
        if (inps.charAt(i) == '1') {
            sb.append("1");
        } else if (inps.charAt(i) == '0') {
            sb.append("0");
        } else if (inps.charAt(i) == 'X') {
            sb.append("x");
        } else if (inps.charAt(i) == 'x') {
            sb.append("x");
        } else if (inps.charAt(i) == ' ') {
            sb.append("x");
        } else if (inps.charAt(i) == '\t') {
            sb.append("x");
            sb.append("x");
        } else {
            throw new Exception("invalid character");
        }
    }
    return sb.toString();
}

输出fromString("1 x \tX 00")后,您会得到预期结果1xxxxxxx00

希望这有帮助