Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out the contents of a LinkedHashMap

Here is my code,

import java.util.LinkedHashMap;
import java.util.Scanner;

public class Project8Q3 extends President {

    private static LinkedHashMap<String, President> presidents;

    public static void main(String[] args) {
        String selection = null;
        Scanner in = new Scanner(System.in);
        do {
            presidents = new LinkedHashMap<String, President>();

            addPresident("16", "Lincoln", "Abraham", null);
            //a few more president's info but you get the idea

            System.out.println("******************************************");
            System.out.println("* G - Get a president's information      *");
            System.out.println("* D - display all president's information*");
            System.out.println("* X - Exit the  program                  *");
            System.out.println("******************************************");
            System.out.println("Enter your selection: ");

            selection = in.next();

            if (selection.trim().equalsIgnoreCase("G")) {
                System.out.println("Enter the president's ID: ");
                showPresident(in.next());
            } else if (selection.equalsIgnoreCase("D")) {
                showPresidents();
            } else if (selection.equalsIgnoreCase("X")) {
                System.out.println("\nThank you for using the PRESIDENT DATABASE. Good-bye!");
            } else {
                System.out.println("INVALID SELECTION! TRY AGAIN!\n");
            }
        } while (!selection.equalsIgnoreCase("X"));
        in.close();
    }

    public static void addPresident(String id, String lastName,
            String firstName, String middleInitial) {
        President president = new President(id, lastName, firstName, middleInitial);
        presidents.put(president.id, president);
    }

    public static void showPresidents() {
        //presidents = new LinkedHashMap<String, President>();

    }

    private static void showPresident(String string) {
        // TODO Auto-generated method stub

    }

}

I am trying to print out the president's information, in the "showPresidents()" function. (clearly). I'm taking too many programming language course right now and I think I'm confused about how to print out the LinkedHashMap. Can anyone help me out?

like image 526
Byron R. Boydstun Jr. Avatar asked Sep 18 '25 11:09

Byron R. Boydstun Jr.


1 Answers

To print a particular President given the name, you need to access the presidents map using the name as the key, and print out the value. To print out all presidents, we can loop over keys in the presidents map as follows:

public static void showPresidents() {
    for (String key : presidents.keySet()) {
        showPresident(key);
    }
}

private static void showPresident(String string) {
    System.out.println(presidents.get(string));
}
like image 105
k_ssb Avatar answered Sep 20 '25 01:09

k_ssb