Java: bad operand type
java
Solution
There are two problems in your code: you need to use `equals` to compare Java strings, and you need to repeatedly use the comparison to construct an `||` expression:
if(t_city.equals("Judenburg") || t_city.equals("Knittelfeld") ... )
Better yet, construct a `HashSet<String>` of the cities that you wish to match, and use `contains` method to check the condition:
Set<String> cities = new HashSet<String>(Arrays.asList(
"Judenburg", "Knittelfeld", "Zeltweg", "Leoben", "Bruck/Mur", "Kapfenberg"
));
...
if (cities.contains(t_city)) {
...
}
Here is a demo of this later approach on ideone.
Problem
I'm a beginner so sorry if the question is stupid. I have wrote following code: ``` public class Traindata { String City; public Traindata(String t_city) { if(t_city == "Judenburg" || "Knittelfeld" || "Zeltweg" || "Leoben" || "Bruck/Mur" || "Kapfenberg") { City = t_city; } else { System.out.println("City not allowed: " + t_city + "\n"); } ``` What I'm trying to do is to check if t_city is the same as one of the allowed Cities (Judenburg, Knittelfeld, Zeltweg, Leoben). But when I try to compile the code, I get this error-message: "error: bad operand type for binary operator '|'" So can anybodye help me with this? I think I used the "||" wrong, but I just can't get it to work. E: Thanks everyone, I didn't even know equals() existed.