Answer the question
In order to leave comments, you need to log in
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>
Answer the question
In order to leave comments, you need to log in
The fifth condition is also NOT met if g contains "bool". Take a closer look at the conditions:
if(!(g=="bool")) {
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question