Search code examples
javaif-statementboolean-expression

How To Compare Booleans In An If Statement With Output Java


I am creating a Java program for comparing temporary usernames and passwords. I am needing to let the user know rather they got the username and password correct or not. If "checkFinal" is true I need it to print Welcome else print Incorrect Information. For some reason, I can not get it to compare the booleans for some reason. I need this to be in a different method also. I am also open to simplifying the program. All help is Welcome. Thanks In Advance.

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) {
    compile();
}
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;
}
private static boolean checkOne(String validusername, String inputusername) {
    boolean usernamecheck = false;
    if (validusername == inputusername) {
        usernamecheck = true;
    }
    return usernamecheck;
}
private static boolean checkTwo(String validpassword, String inputpassword) {
    boolean passwordcheck = false;
    if (validpassword == inputpassword) {
        passwordcheck = true;
    }
    return passwordcheck;
}
private 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() {
    greeting();
    questiona();
    questionb();
    boolean usernamecheck = checkOne(validusername, inputusername);
    boolean passwordcheck = checkTwo(validpassword, inputpassword);
    checkFinal(usernamecheck, passwordcheck);
}
}

Solution

  • There were are few places to be updated. Here is the updated code. I've put a print true/false at the end based on the check result.

    import java.util.Scanner;
    public class Password {
        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) {
            compile();
        }
        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 validpassword;
        }
        private static boolean checkOne(String validusername, String inputusername) {
            boolean usernamecheck = false;
            if (username().equals(inputusername)) {
                usernamecheck = true;
            }
            return usernamecheck;
        }
        private static boolean checkTwo(String validpassword, String inputpassword) {
            boolean passwordcheck = false;
            if (password().equals(inputpassword)) {
                passwordcheck = true;
            }
            return passwordcheck;
        }
        private static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) {
            boolean checkFinal = false;
            if (usernamecheck && passwordcheck) {
                checkFinal = true;
            } else {
                checkFinal = false;
            }
            return checkFinal;
        }
        public static void compile() {
            greeting();
            questiona();
            questionb();
            boolean usernamecheck = checkOne(validusername, inputusername);
            //System.out.println(usernamecheck);
            boolean passwordcheck = checkTwo(validpassword, inputpassword);
            //System.out.println(passwordcheck);
            System.out.println(checkFinal(usernamecheck, passwordcheck));
        }
    }