vendredi 18 octobre 2019

Why does the input of string password return null when used in another class

The loop do-while is a loop that should run at least once and then logging all output of the code from inside that { }. The case of do-while for me is not a valid input and would not run the actual input and would output NULL.

import java.util.Scanner;

import static java.lang.System.*;
class PasswordCheck
{
    public String password;
    public String ag;
    public void PasswordCheck()
    {
        do {
            password = ag;
            if(password == "1234") {
                System.out.println("success");
            }else {
                System.out.println("FAIL" + password);
                PasswordCheck d = new PasswordCheck();
                d.check();
            }
        } while (password != "1234");
        if(password == "1234") {
            System.out.println("success");
        }
    }
    public void check()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the password:");
        String ag = keyboard.next();
        System.out.println(ag); 
        PasswordCheck as = new PasswordCheck();
        as.PasswordCheck();
    }
}
public class PassRunner
{
    public static void main( String args[] )
    {
        PasswordCheck test = new PasswordCheck();
        test.check();
     } 
}

I have already tried re putting the type of class and also putting password = ag;

Output:

Enter the password:1234

1234

FAILnull

Enter the password:1234

1234

FAILnull

Enter the password:

Aucun commentaire:

Enregistrer un commentaire