1

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; } } 
7
  • 2
    The code you've provided does not reproduce the behavior you've observed. ideone.com/CZM804 Please provide a minimal but complete example which reproduces the problem. Commented Apr 7, 2015 at 5:23
  • I assume it should be enough what the method does. Or not? @Radiodef Commented Apr 7, 2015 at 5:25
  • 1
    @Yuri This code does not behave as you described. Either you described it wrong or you aren't running this code. Commented Apr 7, 2015 at 5:33
  • Folks, I'm testing my professor's test cases with JUnit. But does the logic of the code look fine? @EJP Commented Apr 7, 2015 at 5:35
  • As per my earlier comment, the method you've shown us works. The error is elsewhere or the code you've shown us is different. Commented Apr 7, 2015 at 5:42

2 Answers 2

3

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]

Sign up to request clarification or add additional context in comments.

1 Comment

It’s good that you renamed the variable from expected to actual to represent it’s real role but you should also swap the parameters of assertEquals as (assuming it’s JUnit) the first parameter represents the expected value and the second the actual. Otherwise, the resulting exception’s message will be as confusing as demonstrated by this question. As a side note, Arrays.asList doesn’t require manual array creation. Arrays.asList(Signal.HI, Signal.X, …) will do as well.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.