Multiple boolean values in if else statement is always false in Java - java

I am stuck to this method because of the if else condition says that
Condition usersInSales && usersInPayments is always false
Condition usersInSales && usersInLoans is always false
Condition usersInPayments && usersInLoans is always false
I tried different condition combinations and added the false values to try resolve it but it didn't help. Please can I have some help? Thanks in advance
private List<UserResource> usersFilteredByDepartment(List<UserResource> users, boolean usersInSales, boolean usersInPayments, boolean usersInLoans) {
if (usersInSales) {
return getUsersInSales(users);
} else if (usersInPayments) {
return getUsersInPayments(users);
} else if (usersInLoans) {
return getUsersInLoans(users);
} else if (usersInSales && usersInPayments) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInSales && usersInLoans) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInPayments && usersInLoans) {
return Stream.concat(getUsersInPayments(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else return users;
}

You have two solutions.
Reorder your conditions, as others have shown. In your code by time you hit the && statements you have already dealt with the cases when one half is true. The && (two clauses) is more restrictive than a single clause.
Alternatively, put the double clauses inside the previous ifs.
if (usersInSales) {
return getUsersInSales(users);
} else if (usersInPayments) {
return getUsersInPayments(users);
} else if (usersInLoans) {
return getUsersInLoans(users);
} else if (usersInSales && usersInPayments) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInSales && usersInLoans) {...
becomes
if (usersInSales) {
if (usersInPayments) { // Nested if is like && operator.
return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
} else {
return getUsersInSales(users);
}
} else if (usersInPayments) { ...
This is a little more efficient and I think generally preferable.

if (usersInSales) {
return getUsersInSales(users);
} else if (usersInPayments) { ---> You get here when userInSales = false
return getUsersInPayments(users);
} else if (usersInLoans) { --> You get here when usersInSales = false && usersInPayments = false
return getUsersInLoans(users);
} else if () { --> You get here when usersInSales = false && usersInPayments = false && usersInLoans = false. No use in comparing what you compare here. It will be always false as it reports to
}
Hope you now can figure your way out
You could try to check first the most specific constraints and then in the end move to more general constraints.
if (usersInSales && usersInPayments) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInSales && usersInLoans) {
return Stream.concat(getUsersInSales(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInPayments && usersInLoans) {
return Stream.concat(getUsersInPayments(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
} else if (usersInSales) {
return getUsersInSales(users);
} else if (usersInPayments) {
return getUsersInPayments(users);
} else if (usersInLoans) {
return getUsersInLoans(users);
} else return users;
Checking your workflow however IMO your method would have more sense without all those if, else checks. Just this could be enough
private List<UserResource> usersFilteredByDepartment(List<UserResource> users, boolean usersInSales, boolean usersInPayments, boolean usersInLoans) {
return Stream.concat(
usersInPayments? getUsersInPayments(users).stream(): Stream.empty(),
usersInLoans? getUsersInLoans(users).stream(): Stream.empty(),
usersInSales? getUsersInSales(users).stream(): Stream.empty()
).distinct().collect(Collectors.toList());
}

Consider your logic: you first check usersInSales, and return if it's true. Then you check usersInPayments and return if it's true. So when you then check usersInSales && usersInPayments, both will be false because you already handled the cases when either of them are true.
You need to change your logic so you handle the conditions additive instead of exclusively.

your last 3 conditions will never run. because one of your first 3 conditions will already be true. To fix this you need to reverse the order of your conditions, try this:
if (usersInSales && usersInPayments) {
} else if (usersInSales && usersInLoans) {
} else if (usersInPayments && usersInLoans) {
} else if (usersInSales ) {
} else if ( usersInLoans) {
} else if (usersInLoans) {
} else return users;

Related

In Java how would I reference a whole object within a its own class?

I have this in a class called Pokemon and what I would like to do is reference the current object as a whole and pass it through int .use. (I know that you can reference specific pieces of data based on the object using it so I would assume you could do it for the object as a whole) Any help would be greatly appreciated.
public boolean attack(int m,Pokemon p) {
boolean hit = true;
//check what move the use and take away a pp and return it
if (m == 1) {
hit = m1.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 2) {
hit = m2.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 3) {
hit = m3.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 4) {
hit = m4.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else {
return true;
}
}
You use keyword this to refer to (the whole of) the current object.
As the JLS says it:
Keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked.

Why is my code returning true for cases that should return false? [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 6 years ago.
I am writing a code that will import a string of characters from a text file, analyze the string using a stack and determine which "language" the string belongs to based on whether or not it fits certain rules. The code below tests to see if an input follows the pattern (A^nB^)^p (where n is greater than or equal to 0). The way I have it written is to load the first set of A's and B's onto a stack, then load the second set of A's and B's onto another stack, pop the two stacks at the same time and compare the returned values. If they match, move on (until the two stacks empty at the same time, hopefully) if they don't then return false.
public static boolean checkL4(File file) throws IOException
{
Stack stack1 = new Stack();
Stack stack2 = new Stack();
Stack stack3 = new Stack();
boolean firstCompare = true;
boolean bStart = false;
char w = 0;
try (Scanner sc = new Scanner(file).useDelimiter("\\s*"))
{
while (sc.hasNext()){
w = sc.next().charAt(0);
if (w == 0) {
return true;
}
if (w != 'A' && w != 'B')
{
return false;
}
if (w == 'A') {
if(!bStart) {
stack1.push(w);
stack3.push(w);
}
if(bStart && stack2.isEmpty()) {
stack2.push(w);
} else {
if (firstCompare) {
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (!stack1.isEmpty() && stack2.isEmpty())
{
return true;
}
if (stack1.isEmpty() && !stack2.isEmpty()) {
return false;
} else {
if (stack1.pop() == stack2.pop()) {
return true;
} else {
return false;
}
}
}
stack1.push(w);
firstCompare = false;
} else {
if(stack1.isEmpty()){
while (!stack3.isEmpty() || !stack2.isEmpty()) {
if (stack3.isEmpty() && !stack2.isEmpty()) {
return false;
} else {
if (stack2.isEmpty() && !stack3.isEmpty()) {
return false;
} else {
if (stack3.pop() == stack2.pop()) {
return true;
} else {
return false;
}
}
}
}
stack1.push(w);
}
if (stack3.isEmpty()){
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (stack1.isEmpty() && !stack2.isEmpty()) {
return false;
} else {
if (stack2.isEmpty() && !stack1.isEmpty()) {
return false;
} else {
if (stack1.pop() == stack2.pop()) {
return true;
} else {
return false;
}
}
}
}
stack1.push(w);
}
}
}
}
if (w == 'B') {
bStart = true;
if(bStart && stack2.isEmpty()) {
stack2.push(w);
}
if(bStart && !stack2.isEmpty()) {
if (!stack1.isEmpty()) {
stack3.push(w);
}
if(!stack3.isEmpty()) {
stack1.push(w);
}
}
}
}
}
return false;
}
This code works correctly for most inputs (returning true for AB and AABBBAABBB, and returning false for BBAA) but in some cases where it should return false (like ABBA and AABBCCD) it returns true. So why is it returning true for cases that are palindromes and cases where there are non A and non B letters. I know I have a statement in there that states if w (the input) is not and A and not a B, return false. This is worked in similar methods I have written, so why not this one? I have also written this to return false if the two returned values don't match (which they shouldn't if the input is a palindrome).
I think you should use .isEqual to compare between two Strings instead of using ==

Method always returns true?

I have a method that is being called to validate that an IP address is assignable. No matter what I pass to it, it is always returning true. What do I need to set the return as to get this method working properly?
public boolean checkValidIPClass(String x) {
for (int i = 0; i < 4; i++) {
try {
if (retString.equals("A")) {
if ((intParts[1] == 0) && (intParts[2] == 0) && (intParts[3] == 0))
return false;
if ((intParts[1] == 255) && (intParts[2] == 255) && (intParts[3] == 255))
return false;
}
if (retString.equals("B")) {
if ((intParts[2] == 0) && (intParts[3] == 0))
return false;
if ((intParts[2] == 255) && (intParts[3] == 255))
return false;
}
if (retString.equals("C")) {
if (intParts[3] == 0)
return false;
if (intParts[3] == 255)
return false;
}
} //ends try
catch (NumberFormatException nfe) {
return false;
} //ends catch
} //ends for
return true;
}
retString is making it to the method and is a string that was returned from another method that checks what class the IP address is and assigns it, this was verified with a print statement. Thanks!
EDIT: How has this been answered and downvoted? My question wasn't about comparing the strings, it was about the method always returning true even when I know the if statements should be catching the error and returning false?
EDIT2: Updated my code.
I don't get why you're doing a loop, but you could try this:
public boolean checkValidIPClass(String ipClass, String ipAddress)
{
if (ipClass.contentEquals("A"))
{
if (ipAddress.endsWith("0.0.0") || ipAddress.endsWith("255.255.255"))
return false;
}
else if (ipClass.contentEquals("B"))
{
if (ipAddress.endsWith("0.0") || ipAddress.endsWith("255.255"))
return false;
}
else if (ipClass.contentEquals("C"))
{
if (ipAddress.endsWith("0") || ipAddress.endsWith("255"))
return false;
}
return true;
}
Since you're just checking the ending array parts of the IP address, you don't need to break it into an array, just leave it as a string.
And keep in mind that this would only satisfy IPv4 formatted IP addresses. It will not work for IPv6 formatted addresses

Java nested ifs

Below is a small function which when given two numbers (a, b), returns true if one of the numbers is a teen-number. Returns false if both are teen. Returns false if both are not teen. I failed these test cases but I can't figure out why. Help?
(13, 99), (14, 20), and (16, 9)
public boolean loneTeen(int a, int b)
{
if(a<=19 && a>=13)
{
if(b<=19 && b>=13)
{
return false;
}
}
else if(a<=19 && a>=13)
{
return true;
}
else if(b<=19 && b>=13)
{
return true;
}
return false;
}
All three test cases will enter first if branch, they will not match inner condition and, since they already matched first branch, will not match any of else if's. So, they will all return false which is wrong.
Using a small auxiliary method can make your life much easier (and the code more readable!):
private boolean isTeen(int a) {
return a > 12 && a < 20;
}
public boolean loneTeen(int a, int b) {
if(isTeen(a) && isTeen(b) ||
!isTeen(a) && !isTeen(b)) {
return false;
}
return true;
}
First else won't be executed as you are putting the same condition on both if and else.try putting
if( a>=13 && a<=19 && b>=13 && b<=19){return false;}
else if(a>=13 && a<= 19){return true;}
else if(b<=19 && b>=13){return true;}
else return false;

Inversion of return value

I have a flag that I want to pass to a function which returns true or false based on a value in a map:
// userList is a List<String> and is stored as the value field in a map
// user is a String
if(flag)
{
if (userList == null)
return false;
else if(userList.size() == 0)
return true;
return userList.contains(user);
}
else
{
if (userList == null)
return true;
else if(userList.size() == 0)
return false;
return !userList.contains(user);
}
My question is this: is there anyway to tidy this code up, there is a lot of replication (the if and else block are identical, except their return values are the opposite of each other).
I'm not a very experienced code, and I'd really appreciate some guidance!
Use the flag value instead of constants.
if (userList == null)
return !flag;
else if(userList.size() == 0)
return flag;
A XOR will serve for the last statement (left as exercise to the reader :-p)
We can move the common processing to its own method, then branch based on the flag variable as follows.
public boolean userExists(String user) {
return userList != null && (userList.size() == 0 || userList.contains(user));
}
...
if(flag) return userExists(user);
else return !userExists(user);
As a side note, you may have a logic error. I'm not sure why you'd want to return true in the case of userList.size() == 0.
Here's a way to simplify the whole snippet of code, removing the outer if/else statement:
if (userList == null)
return !flag;
else if (userList.isEmpty())
return flag;
return userList.contains(user) == flag;
if (userList == null)
return !flag;
else if(userList.size() == 0)
return flag;
return flag ? userList.contains(user) : !userList.contains(user);ยท
You can easily remove duplication by making the return value be a function of flag :
if (userList == null) {
return !flag;
} else if (userList.size() == 0) {
return flag;
}
return !flag ^ userList.contains(user);
return userList != null && userList.contains(user) == flag;
Might do the job.

Categories

Resources