Larson Carter
Larson Carter

Reputation: 163

How To Call A Method With Booleans Passing Through Java

I am creating an example login app. For some reason when I call the method checkFinal in my main method it is giving me an error. It says that it needs to call the booleans which check the username and the password. Which I did. It says that they can not be passed through. I do not know if this is an pass by value or a pass by reference issue. I have all of the other code working.

import java.util.Scanner;

public class program {
    private static Scanner a;
    private static String  inputusername;
    private static Scanner b;
    private static String  inputpassword;
    private static String  validusername;
    private static String  validpassword;
    public static void main(String[] args) {
        greeting();
        questiona();
        questionb();
        username();
        password();
        checkOne(validusername, inputusername);
        checkTwo(validpassword, inputpassword);
        checkFinal(usernamecheck, passwordcheck);
    }
    public static void greeting() {
        System.out.println("Hello!");
        System.out.println("Note: All Things Are Case Sensitive!");
    }
    public static String questiona() {
        System.out.println("What Is Your Username?");
        a = new Scanner(System.in);
        inputusername = a.next();
        return inputusername;
    }
    public static String questionb() {
        System.out.println("What Is Your Password");
        b = new Scanner(System.in);
        inputpassword = b.next();
        return inputpassword;
    }
    public static String username() {
        validusername = "username";
        return validusername;
    }
    public static String password() {
        validpassword = "password";
        return validusername;
    }
    public static boolean checkOne(String validusername, String inputusername) {
        boolean usernamecheck = false;
        if (validusername == inputusername) {
            usernamecheck = true;
        }
        return usernamecheck;
    }
    public static boolean checkTwo(String validpassword, String inputpassword) {
        boolean passwordcheck = false;
        if (validpassword == inputpassword) {
            passwordcheck = true;
        }
        return passwordcheck;
    }
    public static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) {
        boolean checkFinal = false;
        if (usernamecheck == true && passwordcheck == true) {
            checkFinal = true;
        } else {
            checkFinal = false;
        }
        return checkFinal;
    }
    public static void compile(String[] args) {
    }
    public static void server(String[] args) {
    }
}

Upvotes: 0

Views: 1481

Answers (4)

ByteSlinger
ByteSlinger

Reputation: 1617

You are not saving the boolean results so that they can be passed to the next step. But you could pass them into checkfinal() like this:

public static void main(String[] args) {
    greeting();
    inputusername = questiona();
    inputpassword = questionb();
    /* these 2 are not used and may not be necessary
      username();
      password();
    */
    checkfinal(checkOne(validusername, inputusername),
        checkTwo(validpassword, inputpassword));
}

Also, note that you probably should set the global variables with the result of the questiona() and questionb() methods as I did. It will work as you have it but it is a bad habit.

Upvotes: 0

clinomaniac
clinomaniac

Reputation: 2218

From the other answers, you should get an idea of what you need to do to fix your error. One other way is to shorten your method to call the checkFinal method with the other two methods as parameters so you don't need to create another variable.

checkFinal(checkOne(validusername, inputusername), 
           checkTwo(validpassword, inputpassword));

A few additional comments about your code:

The method:

public static boolean checkOne(String validusername, String inputusername) {
    boolean usernamecheck = false;
    if (validusername == inputusername) {
        usernamecheck = true;
    }
    return usernamecheck;
}

can be changed to:

public static boolean checkOne(String validusername, String inputusername) {
    boolean usernamecheck = validusername.equals(inputusername);
    return usernamecheck;
}

First thing is that you cannot compare two string using ==. Second thing, you don't need to compare boolean == true. When you say if (boolean) it is implied that it means if (boolean == true). Same goes for your other methods as well.

For example:

public static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) {
    boolean checkFinal = false;
    if (usernamecheck == true && passwordcheck == true) {
        checkFinal = true;
    } else {
        checkFinal = false;
    }
    return checkFinal;
}

can be written as:

public static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) {
    return usernamecheck && passwordcheck;
}

Upvotes: 1

ktzr
ktzr

Reputation: 1645

usernamecheck is a local variable in checkOne

passwordcheck is a local variable in checkTwo

in your main checkFinal(usernamecheck, passwordcheck); the two arguments are not initialised.

it looks like you want to pass the outputs of checkOne and checkTwo as the arguments

public static void main(String[] args) {
    greeting();
    questiona();
    questionb();
    username();
    password();
    boolean usernamecheck = checkOne(validusername, inputusername);
    boolean passwordcheck = checkTwo(validpassword, inputpassword);
    checkFinal(usernamecheck, passwordcheck);
}

Upvotes: 1

Krzysztof Majewski
Krzysztof Majewski

Reputation: 2534

You have to assign these two method results to variables:

boolean usernamecheck = checkOne(validusername, inputusername);
boolean passwordcheck = checkTwo(validpassword, inputpassword);
checkFinal(usernamecheck, passwordcheck);

Upvotes: 2

Related Questions