T
T
TheCoolKuid2014-09-07 16:32:26
Java
TheCoolKuid, 2014-09-07 16:32:26

Why is only the last if executed?

Hello! I am writing a simple notebook in Java. I want to do syntax highlighting. I write code like this:

<code lang="java">
String g = j.getText();
          	//Переменные
          	if(!(g=="char")){
          		String c = g.replace("char", "<font size=\"4\" color=\"blue\">char</font>");
          		j.setText(c);
          	}if(!(g=="int")){
          		String c2 = g.replace("int", "<font size=\"4\" color=\"blue\">int</font>");
          		j.setText(c2);
          	}if(!(g=="float")){
          		String c3 = g.replace("float", "<font size=\"4\" color=\"blue\">float</font>");
          		j.setText(c3);
          	}if(!(g=="double")){
          		String c4 = g.replace("double", "<font size=\"4\" color=\"blue\">double</font>");
          		j.setText(c4);
          	}if(!(g=="bool")){
          		String c5 = g.replace("bool", "<font size=\"4\" color=\"blue\">bool</font>");
          		j.setText(c5);
          	}
</code>

But only the last one is executed, that is, bool, if you enter, for example, char , then the code is not highlighted in blue.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vdem, 2014-09-07
@TheCoolKuid

The fifth condition is also NOT met if g contains "bool". Take a closer look at the conditions:

if(!(g=="bool")) {
    ...
}

O
Odissey Nemo, 2014-10-28
@odissey_nemo

str1 == str2Better to use insteadstr1. equals(str2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question