0

I'm learning how to use a hashmap. I am trying to build an ATM program which allow the user to log in if they enter their card number and pin number correctly. Below is my code.

import java.util.*;
import java.util.Scanner;
import java.util.Iterator;

public class Main {

    public static void main(String[] args) {
    // write your code here
        Map<String, String> map = new HashMap<>();
        map.put("123456789", "123456");
        map.put("987654321", "654321");

        Scanner in = new Scanner(System.in);
        System.out.println("Enter Your Card Number");
        String card = in.next();
        System.out.println("Enter Your Pin Number");
        String pin = in.next();
        String everything = card  + "=" + pin;

        Iterator i = map.entrySet().iterator();
        while(i.hasNext()) {
            String s = (String) i.next();
            if(everything == i.next()) {
                System.out.println("Congratulations for logging in");
            } else {
                System.out.println(i.next());
                System.out.println("Wrong Card Number Or Pin");
                System.out.println("Enter Your Card Number");
                card = in.next();
                System.out.println("Enter Your Pin Number");
                pin = in.next();
                everything = card + "=" + pin;
            }
        }

    }
}

My question is how do I check is the user input is the same as one of the values I have entered in my map? Thank you.

  • 1
    Start by using generics: it's `Iterator>`. Next, read https://stackoverflow.com/q/513832/869736. Finally, read the documentation on [`Map.containsKey`](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#containsKey-java.lang.Object-). – Louis Wasserman May 26 '22 at 01:53
  • 3
    Not the question being asked - but note that every time you call 'next()' on an iterator, you're getting a different value. Because each time, you get the next one. – dangling else May 26 '22 at 02:04
  • Also, https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – chrylis -cautiouslyoptimistic- May 26 '22 at 02:29

2 Answers2

3

Map#get

As a rule of thumb, if you're iterating through a map looking for a particular entry, you're doing it wrong. The whole point of a map is that you can look up a key directly. Call Map#get, pass the key, get back the value mapped to that key.

I assume the Map maps from card number to PIN. On that basis:

boolean valid = false;
Scanner in = new Scanner(System.in);
while (!valid) {
    System.out.println("Enter Your Card Number");
    String card = in.next();
    System.out.println("Enter Your Pin Number");
    String pin = in.next();
    String actualPin = map.get(card);
    valid = pin.equals(actualPin);
    if (!valid) {
        // card number not found
        // or pins do not match
        System.out.println("Try again");
    }
}

By the way, you should add more code to check for null. If the user submits a card number not currently found in our Map, a null is returned.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
dangling else
  • 438
  • 2
  • 3
0

I think maybe you're over thinking this a little. ALl you need to do is simply check firstly if the card number (key) is in the hashmap and if so if the pin matches.

if (map.containsKey(card) && map.get(card).equals(pin)) {
    System.out.println("Congratulations for logging in");
} else {
    System.out.println("I'm sorry your card number or pin was incorrect");
}

That should work for you, I recomend you read the documentation on HashMaps to get a better feel for them: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

Equinox
  • 120
  • 2
  • 8