1

I'm working on little "project" for saving some data in a Hashmap which I want to print out later in my console. I've almost finished with the whole code but I'm having problems with giving it out in my console... My code so far is:

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

public class Passwordsaver` {


        public static void main(String[] args) throws InterruptedException {

            // declare the hashmap
            HashMap<Integer, String> Password = new HashMap<>();
            boolean loopAgain = true;
            Scanner scan = new Scanner(System.in);

            // loop while user not entering no
            do {
                // ask for account (page)
                System.out.print("Enter Page:");
                String page = scan.nextLine();

                // ask for password
                System.out.print("Enter Password");
                String password = scan.nextLine();

                // add the key value pair from user input to the hashmap

                String oldVal = password + page;

                if (oldVal!=null) {
                    System.out.println("The password for the page: " + page + " is "
                            + password + " and will be overwritten if entered again");
                }

                // ask user to check if another entry is required
                System.out.print("Enter another account (y/n)?");
                String answer = scan.nextLine();

                // condition to satisfy in order to loop again
                loopAgain = (answer.equals("y") || answer.equals("Y"));

            } while (loopAgain);
            scan.close();

            System.out.println("\n**********************************");
            System.out.println("The following accounts are in database");
            System.out.println("   account  "+ "      password");       
            for(int page:Password.keySet()){
                System.out.println("   "+ Password +"     "+Password.get(page));
            }
            System.out.println("\n**********************************");
        }

    }

Everything works except the last step...How can I print it? Is there also a possibility to save the data so at a later time I can still change the map without having Eclipse opened the whole time?

Many thanks in advance!!!

Default
  • 13
  • 3
  • what's not working? what error are you getting? you need to provide more details. – takecare Jan 25 '20 at 09:40
  • When I want to print it out in my console, the map doesn't get printed. I want to have all my pages and passwords I entered in my console in the end – Default Jan 25 '20 at 09:51

2 Answers2

0

You are printing the full HashMap (Password) and not the key of every entry. Have a look at the following loop:

 for(int page:Password.keySet()){
                System.out.println("   "+ page +"     "+Password.get(page));
}

Here we print the key and the value of the entry.

In terms of performance, it's better to iterate over the entrySet, so you do not have the additional cost of a look up:

for(Map.Entry<Integer,String> entry:Password.entrySet()){
                    System.out.println("   "+ entry.getKey() +"  " + entry.getValue());
    }

EDIT

In addition you forgot to store the page/password combination in your hashmap. Add Password.put(page, password) to store the data.

You also have to change the type to the HashMap to HashMap<String,String>

osanger
  • 2,276
  • 3
  • 28
  • 35
  • Thank you very mich for your help! I've changed it with for(Map.Entry entry:Password.entrySet()){ System.out.println(" "+ entry.getKey() +" " + entry.getValue()); } But still it's not getting printed... – Default Jan 25 '20 at 09:49
  • Do you want to print all entries with each loop? – osanger Jan 25 '20 at 10:23
  • You forgot to add a password.put() to add the entry to the password hash map – osanger Jan 25 '20 at 10:25
  • Yes exactly, I want to print all entries but it's not printing a single one – Default Jan 25 '20 at 10:25
0

The following code will work:

Map<Integer, String> passwords = ...
for (Map.Entry<Integer, String> entry : passwords.entrySet()) {
    //  iterates over every entry in the map, creates a variable called "entry"

    int key = entry.getKey();
    String value = entry.getValue();

    //  now you can print with whatever formatting you want. e.g.:
    System.out.println("account: " + key + ", password: " + password);

}

Or, using Java 8 functional style code:

passwords.forEach((key, value) -> {
    System.out.println("account: " + key + ", password: " + password);
});

If you want to store the data, take a look JSONs, and the Jackson Databind library. This is a library to map Java objects to JSONs, and back (this isn't all it does, but just one of the features). In short, you can convert your Map to and from a String, so that it can be saved to a file/sent over the network/etc.

cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • Thank you very much for your help!!! I've changed the code to the following now: `for (Map.Entry entry : Password.entrySet()) { int key = entry.getKey(); String value = entry.getValue(); System.out.println("account: " + key + ", password: " + value);` – Default Jan 25 '20 at 10:06
  • But still in my console I just have the output: `Enter Page:YouTube Enter PasswordPasswordfortest The password for the page: YouTube is Passwordfortest and will be overwritten if entered again Enter another account (y/n)?n ********************************** The following accounts are in database account password **********************************` – Default Jan 25 '20 at 10:07
  • I don't get my key nor the value printed :( – Default Jan 25 '20 at 10:09
  • Make sure you `put` your key-value pair in the `Map`. Both of these methods do something *for every item in the map*. If there are no items in the map, they will do nothing. – cameron1024 Jan 25 '20 at 19:58
  • Thank you for your help! I added the put and now everything is working perfectly!!! Thank you very much!!! – Default Jan 26 '20 at 11:45