How to insert various user input values into Java dictionary?

Asked

Viewed 102 times

0

I want to allow multiple pairs (key, value) to be inserted into a dictionary, in Java, from user-typed values. I made a loop for to try to insert but could not, because every time a new pair(key, value) is inserted, overwrite the previous one. Is it possible to do this? Allow multiple entries in that dictionary?

The code below creates the dictionary phoneBook and adds, or at least tries, new pairs, through the variables name and phone.

    Map<String, Integer> phoneBook = new HashMap<String,Integer>();
    int i, phone = 0;
    String name = " ", phoneNumber = " ";

    int n = reading.nextInt(); // Quantidade de casos de teste
    for(i = 0; i < n; i++){
        name = reading.next();
        phone = reading.nextInt();
        phoneBook.put(name, phone);
    }

This part inserts a new String which will allow a search to take place. If the value is found, it prints, or at least should, the values consulted by the user:

    while(reading.hasNext()){

        String s = reading.next();

        if(phoneBook.containsKey(s)){
            System.out.println(s + "=" + phoneBook.get(name));  
        } else{
            System.out.println("Not Found");
        }
    }

Examples of input and output

It starts with entering an integer number for the number of test cases, followed by user entries, then moves on to the search. If the value is found, it is printed. If it is not, a message with "Not Found" will be displayed:

Entree

3
Van 99995555
Fernando 11115555
Marilia 66668888
Van
Emilly
Marilia

Expected Departure:

Van=99995555
Not Found
Marilia=66668888

Only that the output does not come out as expected, it always returns the last number added, when the key is found in the dictionary:

Van=66668888
Not Found
Fernando=66668888

Can someone help me with this? From now on, thank you very much!

  • 1

    Hi Van, it wouldn’t be the case if you read you receive the input of the name variable and the phone variable. Then convert the phone variable to integer. Ai only use . put with both variables. This reply can help you .

  • Hi, @Davi Mello, I didn’t understand what you meant, the already Integer phone variable. Mine is storing more than one pair(key, value) in the phonebook.

  • Like, if I were to enter a couple of times, I would have to write: phonebook.put("van", 33334444);. But I want to enter an indefinite number of times. For example, if user wants to insert three or more different pairs at once: phonebook.put("van", 33334444);/phonebook.put("milestones", 55556666);/phonebook.put("Fernating", 88889999); That’s what I want to do. The problem is that whenever the user inserts a new pair, overwrite the previous one.

  • 1

    The map only overwrites if you use the same key. Ex: if you do phoneBook.put("van", 33334444); and then phoneBook.put("van", 99991111);, it will overwrite the previous value because the key ("van") is the same. But if the keys are different (as in your example: phoneBook.put("van", 33334444);/ phoneBook.put("marcos", 55556666);/ phoneBook.put("fernando", 88889999);, none of them is overwritten. Please click [Edit] and put some examples to make more clear what is actually happening, because only with the past information is not very clear which is the problem.

  • Okay, @hkotsubo! I’ll edit! Thank you!

  • 1

    No while, instead of System.out.println(s + "=" + phoneBook.get(name)), do System.out.println(s + "=" + phoneBook.get(s))

  • 1

    @hkotsubo worked, thank you very much! ^_^

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.