mercredi 5 juillet 2017

Accessing variables from other class - If-Statment does not work

I have two classes, both "scanning" a name and a password from the user. When I try to check if they are correct, my if-statement always returns "false".

public class User{
   Scanner scan = new S...
   String username, pw;

   public void regis(){
      username = scan.nextLine();
      pw = scan.nextLine();
   }
   public String getUsername(){
      return username;
   }
   //same for getPw()..
}

public class Check{
   Scanner scan2 = new ...;
   User usr = new User();

   public void check(){
      String name, pass;
      System.out.print("name?");
      name = scan.nextLine();
      System.out.print("password?");
      pass = scan.nextLine();

      if(name.equals(usr.getUsername()) && pass.equals(usr.getPw())){
         return true;
      }else{
         return false;
      }
   }
}

I always get "false" as result. I tried to set username and pw from class User to static and it worked. Why is my solution not working?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire