0

How to compare object class to multiple integers?

if(p.getBornyear()==2000) && (p.getBoenyear()==2002) && (p.getBornyear()==2003){ System.out.println("Generation z"); } else { System.out.println("Invalid") } When i enter 2003 the output is invalid i dont know why?

4th Sep 2018, 1:15 AM
Vance Sibonga Pedroso
Vance Sibonga Pedroso - avatar
3 Answers
+ 1
Vance Sibonga Pedroso If you want your comparison to be true if your variable has any value within a list, you should use the 'or' operator (||). But your problem seems to require comparison with a range. Why not something like: if (var >= minimum && var <= maximum) { commands; }
4th Sep 2018, 11:25 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Because it's different from 2000 and 2002. Since you're using the 'and' operator (&&), the conditional only gets true if the attribute 'Bornyear' equals 2000, 2002 and 2003 at the same time - that is: never. By the way, watch the typo in the second line.
4th Sep 2018, 1:22 AM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado so what is the best way or method to compare object class to multiple integers like this?
4th Sep 2018, 1:43 AM
Vance Sibonga Pedroso
Vance Sibonga Pedroso - avatar