Resolving PMD Error - java

I am using PMD for checking Coding Standards
I am confused about the following below point where PMD is showing Error
A method should have only one exit point, and that should be the last statement in the method
Currently inside my Method it is as
public boolean validate()
{
if (length == 4) {
return true;
if (length == 2) {
return false;
else
return false ;
return true ;
}
Is my code is wrong ?? and please tell me how can we chage this in case its wrong ??

Example :
public boolean foo(int i) {
if (i > 0) {
return true; // Multiple exit points
}
return false; // ~ ~ ~
}
public boolean bar(int i) {
boolean bool = false;
if (i > 0) {
bool = true;
}
return bool; // Single exit points
}
See OnlyOneReturn.
See question
Programming preference - use else ifs with multiple return statements?

Related

Need help checking if a binary tree is a valid binary search tree in Java [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 3 years ago.
Here is my code:
public boolean isBST() {
return isBST(this.root);
}
private boolean isBST(BinaryNode<T> rootNode) {
if (rootNode == null) {
return false;
}
if (rootNode.isLeaf()) {
return true;
}
T left = null;
T right = null;
if (rootNode.hasLeftChild()) {
left = rootNode.getLeftChild().getData();
if (left.compareTo(rootNode.getData()) < 0) {
return this.isBST(rootNode.getLeftChild());
}
else {
return false;
}
}
if (rootNode.hasRightChild()) {
right = rootNode.getRightChild().getData();
if (right.compareTo(rootNode.getData()) > 0) {
return this.isBST(rootNode.getRightChild());
}
else {
return false;
}
}
return true;
}
This code works for simple binary trees, but it doesn't work for other ones. Such as if I have a tree like so:
5
/ \
3 6
/\
1 2
It won't mark it false even thought it should since 2 is smaller than 3 and is in the wrong place. My code just checks the left child's left children, and the checks the right child's right children and not the inner children. What can I do to make this code work in the way that I wrote it? How can I modify it?
The bug is in the line
return this.isBST(rootNode.getLeftChild());
You must not exit the method just because the left part is checked (if it exists).
Instead of
if (left.compareTo(rootNode.getData()) < 0) {
return this.isBST(rootNode.getLeftChild());
}
else {
return false;
}
this should do the expected:
if (left.compareTo(rootNode.getData()) >= 0 ||
!this.isBST(rootNode.getLeftChild()) {
return false;
}
(For symmetry reasons you may also want to rewrite the right part check in a similar way, however this is not required. Your could would also work in its current form.)
I think all you need to do is when you recursively look left and right only return if it's false
if (rootNode.hasLeftChild()) {
left = rootNode.getLeftChild().getData();
if (left.compareTo(rootNode.getData()) < 0) {
boolean isValid = this.isBST(rootNode.getLeftChild());
if (!isValid) {
return false;
}
} else {
return false;
}
}
if (rootNode.hasRightChild()) {
right = rootNode.getRightChild().getData();
if (right.compareTo(rootNode.getData()) > 0) {
boolean isValid = this.isBST(rootNode.getRightChild());
if (!isValid) {
return false;
}
} else {
return false;
}
}
return true
It looks like, at a quick look, the return is happening before the right side is checked.
Slightly more succinct version that also supports duplicates appearing only on the right (if you want to allow duplicates on the left then swap the >= in left comparison for <= in the right comparison
if (rootNode.hasLeftChild()) {
left = rootNode.getLeftChild().getData();
if (left.compareTo(rootNode.getData()) >= 0) || !this.isBST(rootNode.getLeftChild()) {
return false;
}
}
if (rootNode.hasRightChild()) {
right = rootNode.getRightChild().getData();
if (right.compareTo(rootNode.getData()) < 0 || !this.isBST(rootNode.getRightChild()) {
return false;
}
}
return true;

Java Method: Integer contains 1

I am trying to create a method that will return true if the inputted integer has a one in it, and false if it does not. The method works correctly when the inputted number doesn't have a one or ends in a one. But if the one is not the last digit in the int, it incorrectly returns false. Any ideas whats wrong? Here is my script:
public static boolean hasOne(int n) {
boolean retval = false;
if (n % 10 == 1) {
retval = true;
} else {
dropLastDig(n);
}
return retval;
}
public static void dropLastDig(int input) {
int newNum = input/10;
if (newNum > 0) {
hasOne(newNum);
}
}
1000 should return true
211 should return true
1 should return true
3 should return false
234 should return false
You can use recursive function, it's faster:
public static boolean hasOne(int n) {
if(n<0) return hasOne(-n); // check for negatives
if(n==0) return false; // exit condition
if (n % 10 == 1) {
return true;
}
return hasOne(n/10);
}
Or cast it to String, and then check:
String.valueOf(n).contains("1");
Just use String.valueOf instead
return String.valueOf(x).contains("1");
try String.valueOf(n).contains("1")

Return multiple values from void and boolean methods

I have the following problem: Having a boolean static method that computes similarity between two integers, I am asked to return 4 results:
without changing the return type of the method, it
should stay boolean.
without updating/using the values of external variables and objects
This is what I've done so far (I can't change return value from boolean to something else, such as an int, I must only use boolean):
public static boolean isSimilar(int a, int b) {
int abs=Math.abs(a-b);
if (abs==0) {
return true;
} else if (abs>10) {
return false;
} else if (abs<=5){
//MUST return something else, ie. semi-true
} else {
//MUST return something else, ie. semi-false
}
}
The following is bad practice anyway, but If you can try-catch exceptions you can actually define some extra outputs by convention. For instance:
public static boolean isSimilar(int a, int b) {
int abs = Math.abs(a-b);
if (abs == 0) {
return true;
} else if (abs > 10) {
return false;
} else if (abs <= 5){
int c = a/0; //ArithmeticException: / by zero (your semi-true)
return true;
} else {
Integer d = null;
d.intValue(); //NullPointer Exception (your semi-false)
return false;
}
}
A boolean can have two values (true or false). Period. So if you can't change the return type or any variables outside (which would be bad practice anyway), it's not possible to do what you want.
Does adding a parameter to the function violate rule 2? If not, this might be a possible solution:
public static boolean isSimilar(int a, int b, int condition) {
int abs = Math.abs(a - b);
switch (condition) {
case 1:
if (abs == 0) {
return true; // true
}
case 2:
if (abs > 10) {
return true; // false
}
case 3:
if (abs <= 5 && abs != 0) {
return true; // semi-true
}
case 4:
if (abs > 5 && abs <= 10) {
return true; // semi-false
}
default:
return false;
}
}
By calling the function 4 times (using condition = 1, 2, 3 and 4), we can check for the 4 results (only one would return true, other 3 would return false).

Missing return Statement of multiple IFs

Where is the problem? If I use a variable it works just fine, but I am missing something.
public boolean xyzThere(String str) {
if (str.length() > 2){
if(str.startsWith("xyz")){
return true;
} else {
for (int i = 1; i < str.length() - 2; i++){
if(str.substring(i, i + 3).equals("xyz") && !str.substring(i - 1, i).equals(".")) {
return true;
} else {
return false;
}
}
}
} else {
return false;
}
}
This condition needs a return statement as the code inside for loop may not be reachable.
else {
for (int i = 1; i < str.length() - 2; i++) {
if (str.substring(i, i + 3).equals("xyz") && !str.substring(i - 1, i).equals(".")) {
return true;
} else {
return false;
}
}
}
Because it might be possible that none of your outer if condition succeed , So Compiler is making sure that your program doesn't get stuck into the function , by throwing the missing return statement error
So try this way :
if(condition){
return false;
}
elseif(condition){
return true;
}
return false; <--- Now compiler is assured that function will return something
Assuming the return type is boolean
The Java compiler will not evaluate the conditions to determine if one of if condition and an else if condition will be guaranteed to run.
Without the conditions, the compiler see the possibility of neither condition being true, and in that case, nothing is returned. There are not return statements past the else if. This occurs even if we see that logically one of them will be true.
Just turn your else if into an else to satisfy the compiler.
else {
return false;
}
Now the compiler will see that there is a return statement in all possible execution paths.
In these cases, sometimes I comment out the condition to let readers know the real intent.
else // if (str.length() < 3)
{
return false;
}
Update:
Something similar is occurring inside the for loop. The compiler won't assume that at least one iteration of a for loop will occur. If the for loop body is never entered, then the else block ends, and the body of the outer if block ends without a return statement`.
You must provide a return statement after the for loop, in case the for loop is never entered.
Now it is compiling:
public boolean xyzThere(String str) {
if (str.length() < 3){
return false;
} else {
if(str.startsWith("xyz")){
return true;
} else {
for (int i = 1; i < str.length() - 2; i++){
if(str.substring(i, i + 3).equals("xyz") && !str.substring(i - 1, i).equals(".")) {
return true;
} else {
}
}
return false;
}
}
}

How do I check if a method returns true or false in an IF statement in Java?

Let's say I have a boolean method that uses an if statement to check whether the return type should be true or false:
public boolean isValid() {
boolean check;
int number = 5;
if (number > 4){
check = true;
} else {
check = false;
}
return check;
And now, I want to use this method as a parameter of an if statement in a different method:
if(isValid == true) // <-- this is where I'm not sure
//stop and go back to the beginning of the program
else
//continue on with the program
So basically what I'm asking is, how do I check what the return type of the boolean method within the parameters of an if statement is? Your answers are deeply appreciated.
Since it's a method, to call it you should use parens afterwards, so your code would then become:
if(isValid()) {
// something
} else {
//something else
}
public boolean isValid() {
int number = 5;
return number > 4;
}
if (isValid()) {
...
} else {
...
}
You should be able to just call the function within the IF condition so:
if (isValid()) {
}else {
}
Since isValid() returns a boolean the condition will be evaluated right away. I have heard it is better form to create a local var just before you test you condition.
boolean tempBoo = isValid();
if (tempBoo) {
}else {
}
- If statement accepts only boolean value.
public boolean isValid() {
boolean check = false; // always intialize the local variable
int number = 5;
if (number > 4){
check = true;
} else {
check = false;
}
return check;
}
if(isValid()){
// Do something if its true
}else{
// Do something if its false
}
if (isValid()) {
// do something when the method returned true
} else {
// do something else when the method returned false
}
You can use :
if(isValid()){
//do something....
}
else
{
//do something....
}
public boolean isValid() {
boolean check;
int number = 5;
if (number > 4){
check = true;
} else {
check = false;
}
return check;
How can you do this whole method without boolean check?
So how to get rid of.. check = true, check = false, return check stuff?

Categories

Resources