V
V
Vyacheslav_Frein2021-08-29 18:53:32
Java
Vyacheslav_Frein, 2021-08-29 18:53:32

Why does the first if block keep executing?

Good evening! Why does the first if block continue to execute in the code below, if if expression[i] is equal to one of the characters in the condition, it must transfer control to the second block at the next iteration of the loop?

Input data - ["1", "2", "+", "4"]

void parse(){
        String firstNumber = "";
        String secondNumber = "";

        for(int i = 0, j = 0; i < expression.length; i++){
            if((expression[i] != "+" && expression[i] != "-" && expression[i] != "*" && expression[i] != "/") && j == 0){
                firstNumber += expression[i];
                System.out.println(firstNumber);
                System.out.println(j);
            }else if(j == 1){
                secondNumber += expression[i];
                System.out.println(secondNumber);
                System.out.println(j);
            }else{
                j = j + 1;
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-08-29
@Vyacheslav_Frein

Because strings in Java don't compare like that. This is described almost on the first pages of any textbook.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question