0

I'm failing the following test case:

@Test (timeout=3000) public void gatenot_0(){
    GateNot g = new GateNot (new Wire("inw"), new Wire("outa"));
    g.feed("0");
    boolean ans = g.propagate();
    assertEquals(sigs1, g.read());
  }

which says "Invalid character" - Exception thrown by the following method:

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

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



        }
        return values;

    }

Everytime I pass something including "0" or "1" it throws Exception So, should I check every letter of String inps as a String instead of charAt(). If yes, how should I check that? Thanks in advance.

CerebralCortexan
  • 299
  • 2
  • 17
Yuri
  • 99
  • 8

1 Answers1

1

To check every letter as a string you could use

Character.toString(inps.charAt(i))

instead of just inps.charAt(i)

I would recommend storing it in a variable for each iteration instead of calling many times.

You could also do

String.valueOf(inps.charAt(i))

There is a shortcut

inps.charAt(i) + ""

And yet another way:

inps.substring(i, i + 1)

Note that if you end of converting each letter to string, you should use the .equals method when comparing them.

For more information about this, you can look at How to convert a char to a String?

Community
  • 1
  • 1
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29