Sean
Sean

Reputation: 9

(JAVA) Parameter and return types error

can someone tell me whats wrong with my code? It gives me an error of "truefalse cannot be resolved into a variable" The question is asking me to return a value of true, if the int number is even, and false, if it is an odd number.

public class number{
    public static void main(String[] args){

        boolean truefalse = isEven(245);
        System.out.print(truefalse);
    }
    public static boolean isEven(int number) {

        if(number%2 == 0){
            boolean truefalse = true;
        }
        else{
            boolean truefalse = false;
        }
        return truefalse;
    }
}

Upvotes: 0

Views: 55

Answers (6)

Jekin Kalariya
Jekin Kalariya

Reputation: 3507

Declare it outside If else block

boolean truefalse = isEven(245);
        System.out.print(truefalse);
    }
    public static boolean isEven(int number) {
     boolean truefalse =false;
        if(number%2 == 0){
            truefalse = true;
        }

        return truefalse;
    }

Upvotes: 0

Shahid
Shahid

Reputation: 2330

Declare boolean truefalse; outside if-else block. It has truefalse variable inside if-else block, which is has no scope outside if-else. So returned variable can not be resolved.

public static boolean isEven(int number) {

boolean truefalse;

    if(number%2 == 0){
        truefalse = true;
    }
    else{
        truefalse = false;
    }
    return truefalse;
}

Simple isEven could be:

public static boolean isEven(int number) {    
    return (number%2 == 0);
}

Upvotes: 0

Arnav Borborah
Arnav Borborah

Reputation: 11807

Currently, the problem you have is declaring truefalse inside the if-else blocks, so the return statements doesn't know what it is (scope issues). Here are some fixes:

Solution 1

You can simplify your isEven() class to this:

public static boolean isEven(int number) {

    if(number%2 == 0){
        return  true;
    }
    else{
        return false;
    }
}

Solution 2

To keep the boolean, do this

public static boolean isEven(int number) {
    boolean truefalse;
    if(number%2 == 0){
        truefalse =  true;
    }
    else{
        truefalse = false;
    }
    return truefalse;
}

Solution 3

To keep the boolean, but minimize code woth boolean logic, do this:

public static boolean isEven(int number) {
    boolean truefalse = false;
    if(number%2 == 0){
        truefalse =  true;
    }
    return truefalse;
}

Solution 4

The most you can simplify you function is like this:

public static boolean isEven(int number) {

    return number % 2 == 0;
}

This would also have the same result.

Upvotes: 2

PKey
PKey

Reputation: 3841

You should read on java variable scope

What is happening in your is isEven method is you are declaring truefalse variable inside if {} and else {}, but outside those brackets, where return is, this variable does not exist.

So, as so many answers mention you should declare the variable in correct scope e.g.

 public static boolean isEven(int number) {
        boolean truefalse;
        if(number%2 == 0){
            truefalse = true;
        }
        else{
            truefalse = false;
        }
        return truefalse;
    }

Upvotes: 0

Richard H.
Richard H.

Reputation: 33

Maybe more readable will be

public static boolean isEven(int number){
  return number%2 == 0? true: false;

}

Upvotes: 0

Amy
Amy

Reputation: 4032

Replace your code with the following.

public class number{
    public static void main(String[] args){

        boolean truefalse = isEven(245);
        System.out.print(truefalse);
    }
    public static boolean isEven(int number) {
        boolean truefalse = false;
        if(number%2 == 0){
            truefalse = true;
        }

        return truefalse;
    }
}

Upvotes: 0

Related Questions