Problems with throwing exception

the method public static List<Signal> fromString(String inps) create and returns the List of Signal values found in the input string. If any characters other than those in "01xX \t"(including 'whitespace') are found, I must raise an ExceptionLogicMalformedSignal value. When I pass 1 x \tX 00 as input it throws an Exception when it should not. What's wrong with the logic of the code? Thanks in advance!NOTE:HI,LO,X are enum objects.

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) != ' ' || inps.charAt(i) != '\t')
                throw new ExceptionLogicMalformedSignal(inps.charAt(i), "Invalid character!");




        }
        return values;

    }
Jon Skeet
people
quotationmark

Look at this condition:

if (inps.charAt(i) != ' ' || inps.charAt(i) != '\t')

That will pass if the character isn't space or if it isn't tab. A single character can't be both space and tab, so at least one of the subconditions will be true. You want && - although I'd extract the call to charAt:

char c = inps.charAt(i);
if (...)
...
else if (c != ' ' && c != '\t')

... or I'd just use a switch statement, which will be clearer IMO:

switch (inps.charAt(i)) {
    case '1':
        values.add(HI);
        break;
    case '0':
        values.add(LO);
        break;
    case 'x':
    case 'X':
        values.add(X);
        break;
    case ' ':
    case '\t':
        break;
    default:
        throw ...;
}

people

See more on this question at Stackoverflow