将来自Scanner的输入与来自HashMap的键和值进行比较

时间:2018-02-06 14:39:15

标签: java hashmap

我正在创建一个应用程序,我需要在其中创建帐户并首先登录。 我认为我可以使用HashMap,其中login是我的密钥,密码是我的价值。

不幸的是,我在登录时遇到了问题。

这是带有负责日志的方法的类。 checkIfLoginDataIsIncorrect

存在问题
void inputLoginAndPassword() {
    System.out.println("Input your login");
    login = input.next();

    System.out.println("Input your password");
    password = input.next();
}

boolean checkIfLoginDataIsIncorrect() {
    if (loginDetails.containsKey(login)                                    
        && loginDetails.get(login).equals(password)) {
        System.out.println("You've logged in.");
        return false;
    }
    else
        System.err.println("Bad login or password");
    return true;
}

Main.java:

AccountMaker accountMaker = new AccountMaker();
AccountLogger accountLogger = new AccountLogger();
//I'VE WRITTEN IT JUST FOR CHECK:
HashMap<String, String> loginDetails = accountMaker.getLoginDetails();
loginDetails.put("lala","papa");

[...]

    case 2:
        while (loopIsTrue) {
            accountLogger.inputLoginAndPassword();
            if(!accountLogger.checkIfLoginDataIsIncorrect()) {
                loopIsTrue = false;
            } else {
                loopIsTrue = true;
            }
        }
        break;

AccountMaker.java:

public class AccountMaker {
    private HashMap<String, String> loginDetails = new HashMap<>();
    private String login, password;
    private Scanner input = new Scanner(System.in);

    [...]

    HashMap<String, String> getLoginDetails() {
        return loginDetails;
    }
}

AccountLogger.java:

public class AccountLogger {
    private AccountMaker accountMaker = new AccountMaker();
    private Scanner input = new Scanner(System.in);
    private HashMap<String, String> loginDetails = accountMaker.getLoginDetails();
    private String login, password;

    void inputLoginAndPassword() {
        System.out.println("Input your login");
        login = input.next();

        System.out.println("Input your password");
        password = input.next();
    }

    boolean checkIfLoginDataIsIncorrect() {
        //I'VE WRITTEN IT JUST FOR CHECK
        System.out.println(login);
        System.out.println(password);
        ///
        if (loginDetails.containsKey(login) && loginDetails.get(login).equals(password)) {
            System.out.println("You've logged in.");
            return false;
        }
        else
            System.err.println("Bad login or password");
        return true;
    }
}

例如,如果我写loginDetails.put("lala","papa");然后我想登录这个acc程序说Bad login or password

2 个答案:

答案 0 :(得分:1)

您遇到的问题是您正在查看错误的HashMap

  • 创建AccountMaker
    • AccountMaker制作了新的HashMap
  • 创建&#39; AccountLogger&#39;
    • AccountLoggerAccountMaker
      • AccountMaker制作了新的HashMap

我们最终得到了AccountMaker的两个不同实例,以及它们的不同HashMaps

尝试使用AccountMaker使用的相同AccountLogger,或将其传递给构造函数

使用构造函数:

//Main.java
AccountMaker accountMaker = new AccountMaker();
AccountLogger accountLogger = new AccountLogger(accountMaker);

//AccountLogger.java
class AccountLogger
{
    private AccountMaker accountMaker;

    public AccountLogger(AccountMaker maker)
    {
        accountMaker = maker;
    }
}

获得相同的AccountMaker

//Main.java
AccountLogger accountLogger = new AccountLogger();
AccountMaker accountMaker = accountLogger.getAccountMaker();

//AccountLogger.java
public AccountMaker getAccountMaker()
{
    return accountMaker;
}

答案 1 :(得分:0)

我正在尝试了解您的问题并编写了以下示例程序以满足您的要求。比较您的代码,看看您是否可以解决您的问题。如果没有,你可以修改下面的代码,让我知道它失败的地方。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Testlogin {
static Scanner input = new Scanner(System.in);
static Map<String, String> loginDetails = new HashMap<String, String>();
static String login,password;


static void inputLoginAndPassword() {
    System.out.println("Input your login");
   login = input.next();

    System.out.println("Input your password");
    password = input.next();
}

boolean checkIfLoginDataIsIncorrect() {
    if (loginDetails.containsKey(login)                                    
        && loginDetails.get(login).equals(password)) {
        System.out.println("You've logged in.");
        return false;
    }
    else
        System.err.println("Bad login or password");
    return true;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    loginDetails.put("lala", "papa");
    inputLoginAndPassword();
    boolean loopIsTrue =true;
    Testlogin testlogin = new Testlogin();
    while (loopIsTrue) {
       // Testlogin.inputLoginAndPassword();
        System.out.println("You have entered login = "+login);
        System.out.println("You have entered password = "+password);
        if(!testlogin.checkIfLoginDataIsIncorrect()) {
              loopIsTrue = false;
        } else {
                inputLoginAndPassword(); // bad login detected so asking for correct details again.
                loopIsTrue = true;
              //
        }
      }
      //break;
}

}