SRL Archive - News and General

Java if statements not working correctly


rj

2013-01-26 00:01:48

Another reason java is annoying to code in. Anyway my code looks like:

public class Test {

static String Description = "bad";

public static void main(String[] args) {
boolean Good;

if (Description == "shit"); {

Good = false;
}

else if (Description == "great"); {
Good = true;
}


if (Good == true); {
System.out.println("Great!");

System.exit(0);

}

else if (Good == false); {
System.out.println("Bad!");

System.exit(0);

}
}
}


And it sais ' error delete token else '

Well, if i delete the else statements then it outputs "Great" no matter what!

x[Warrior]x3500

2013-01-26 00:09:42

no ";" after an if

rj

2013-01-26 00:13:13

public class test {

static String Description = "bad";

public static void main(String[] args) {
boolean Good;

if (Description == "shit") {

Good = false;
}

if (Description == "great") {
Good = true;
}

else {
Good = false;
}

if (Good == true); {
System.out.println("Great!");

System.exit(0);

}

if (Good == false); {
System.out.println("Bad!");

System.exit(0);

}
}
}

Still sais good everytime.

x[Warrior]x3500

2013-01-26 00:31:13


public class test {

static String Description = "bad";

public static void main(String[] args) {
boolean Good;

if (Description == "shit") {
Good = false;
} else if (Description == "great") {
Good = true;
} else {
Good = false;
}
if (Good) {
System.out.println("Great!");
System.exit(0);
} else {
System.out.println("Bad!");
System.exit(0);
}
}
}
^ that should work (dont have a java compiler on me atm :/ )

Echo_

2013-01-26 01:08:48

Don't compare strings with the equality operator, that checks if they point to the same object. use equals instead:

Description.equals("shit")