0

Picture of issue I am reading CSV file and I'm trying to test that yellow color comes after red only when Bit=1... Doesn't && operator suppose to check that both conditions are true? Why does it stops running when it reaches the color Yellow but the Bit is '1' and not '0'

////-> Case 4 - Yellow Light can start only when TrafficBit =1 <-\\\\
private void Yellow_Light_Rule(){

    if (trafficLights.isEmpty())
        throw new IllegalArgumentException("Traffic lights list is empty");

    String currentTrafficLightColor = trafficLines.get(0).split(",")[1];
    String currentBitNum = trafficLines.get(0).split(",")[2];

    for (String trafficLine : trafficLines) {
        String[] trafficParts = trafficLine.split(",");
        int cycleNum = Integer.parseInt(trafficParts[0]);
        String trafficLightColor = trafficParts[1];
        String trafficBit = (trafficParts[2]);


        System.out.println("Traffic cycle:" + cycleNum + ", color:" + trafficLightColor + ", bit:" + trafficBit);


        if (currentTrafficLightColor.equals(trafficLightColor))
            continue;
        if (currentTrafficLightColor.equals("Yellow") && currentBitNum.equals("0"))
            break;

        String nextTrafficLight = getNextTrafficLightCase4(currentTrafficLightColor);
        String nextNextBit = getNextBitCase4(currentBitNum);

        Assertions.assertEquals(nextTrafficLight, trafficLightColor, trafficLightColor+" Is a mismatch between expected and actual color");
        Assertions.assertEquals(nextNextBit, trafficBit, trafficBit+" Is a mismatch between expected and actual bit");
        currentTrafficLightColor = trafficLightColor;
        currentBitNum = trafficBit;


    }
}

private int getBitIndexCase4(String trafficBitNum){
    for (int i = 0; i<trafficLines.size(); i++){
        if (trafficLines.get(i).equals(trafficBitNum))
            return i;
    }

    throw new IllegalArgumentException("Bit number '" + trafficBitNum + "' is not correct");
}

private String getNextBitCase4(String trafficBitNum){
    int bitIndex = getBitIndexCase4(trafficBitNum) +1;
    if (bitIndex >= trafficLines.size())
        bitIndex = 0;
    return trafficLines.get(bitIndex);
}

private int getTrafficIndexCase4(String trafficLightName) {
    for (int i = 0; i < trafficLights.size(); i++) {
        if (trafficLights.get(i).equals(trafficLightName))
            return i;
    }

    throw new IllegalArgumentException("Traffic light name '" + trafficLightName + "' is not correct");
}

private String getNextTrafficLightCase4(String trafficLightName) {
    int trafficIndex = getTrafficIndexCase4(trafficLightName) + 1;
    if (trafficIndex >= trafficLights.size())
        trafficIndex = 0;
    return trafficLights.get(trafficIndex);
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
EXepTioNN
  • 1
  • 3
  • You are checking `currentTrafficLightColor`, not `trafficLightColor` as in your `System.out.println()` statement. – Progman Aug 15 '21 at 15:01
  • 1
    Does this answer your question? [Why do we usually use || over |? What is the difference?](https://stackoverflow.com/questions/7101992/why-do-we-usually-use-over-what-is-the-difference) – joshmeranda Aug 15 '21 at 15:01
  • 1
    It's a short-circuiting operation, it will check only until it finds a case that can satisfy the entire expression. So `false && anything == false`, and `true || anything == true`. In this sense, since you check if the light is `yellow` first, it being `false` would evaluate the entire `&&` expression. – Rogue Aug 15 '21 at 15:01
  • So what do I need to do to check both conditions for it to run correctly? – EXepTioNN Aug 15 '21 at 15:30
  • @EXepTioNN Why do you want both conditions to run when the first condition already returns `false`? The result of the `&&` operator will be `false` anyway. – Progman Aug 15 '21 at 15:48
  • @Progman Because I want it to continue reading the CSV file till it encounters Color=Yellow and Bit=0 and then stops. – EXepTioNN Aug 15 '21 at 17:57
  • @EXepTioNN It will, but `currentTrafficLightColor` has not the value of `Yellow`. Output the values of `currentTrafficLightColor` and `currentBitNum` to see what they are. That way you can see which one operand (or both) is `true` or `false`. – Progman Aug 15 '21 at 17:59

0 Answers0