简体   繁体   中英

Special characters in a string [JAVA]

folks, the method below would throw an Exception if other character than "01xX \\t" (including whitespace and \\t inside a passed String) is found. If I have this String "1 x \\tX 00" , the method should return [1,X,X,X,X,X,X,X,0,0] but Im getting only [1,X,X,0,0] in which the 'whitespace' and '\\t' somehow are not getting included. 'Whitespace' and '\\n' also should return 'X' . Please could smb help me?

    //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;
    }
}

Seem like the assertion is not correct, it's:

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

while it should be :

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

Because the expected result is [1,X,X,X,X,X,X,X,0,0]

here is a modified version of your code that works as you need it...

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

When you output fromString("1 x \\tX 00") you get the result 1xxxxxxx00 as expected.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM