0

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);
}
}
Larson Carter
  • 153
  • 2
  • 11
  • 3
    don't use `==` for Java String comparisons. that is probably the most common mistake beginners make. and it gets posted here a lot. – Patrick Parker May 04 '18 at 02:12
  • @PatrickParker ok, will the program completely not work or what will happen? I have no errors right now. Previously in projects, it has worked fine. – Larson Carter May 04 '18 at 02:14
  • 1
    also there is no need to write `if(myBoolean == true)`, simply write `if(myBoolean)` – Patrick Parker May 04 '18 at 02:14
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Patrick Parker May 04 '18 at 02:17
  • 1
    Use equals to check your username and password instead of == in your checkone and checktwo functions. – Rohit Suthar May 04 '18 at 02:18
  • It says that when I try to compare the booleans with only one = it says it can not pass the booleans through. Is this a pass by reference issue? – Larson Carter May 04 '18 at 02:19

2 Answers2

2

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));
    }
}
Jagrut Sharma
  • 4,574
  • 3
  • 14
  • 19
  • This did answer 95% of my problem. Now how do I get it to print out Welcome if they got the username and password correct and Incorrect Information If they got either the username or password incorrect? – Larson Carter May 04 '18 at 02:25
  • You can add this line at the end: System.out.println(checkFinal(usernamecheck, passwordcheck)? "Welcome" : "Incorrect Information"); – Jagrut Sharma May 04 '18 at 02:29
  • And comment out the last statement in current code: //System.out.println(checkFinal(usernamecheck, passwordcheck)); – Jagrut Sharma May 04 '18 at 02:30
  • "that worked" but did you understand why? don't cheat yourself out of an education by copying answers you don't understand. – Patrick Parker May 04 '18 at 02:58
  • Yea, I partially understand it, I'm still going through my CS classes, I'm going to ask my instructor to explain it better when I see him again. – Larson Carter May 04 '18 at 13:14
1

You need to use equals() to compare two strings. And no need to check for usernamecheck = true. See sample code below and modify accordingly. It will work.

    if (validpassword.equals(inputpassword)) {
            passwordcheck = true;
        }
 ...
 if (usernamecheck  && passwordcheck) {
        checkFinal = true;
    } else {
     ..
}
nan
  • 191
  • 3
  • 10