Java program reading from 2 files. so i'm creating a program for u.s population by state per 2010 census. Below is the question:

create a program that will read from two files and fills two HashMaps to find the population (according to the 2010 census) of an individual state or the United States. There will be 2 text files in your program:

  1. The name of the states and their abbreviations, is a comma separated file - name,abbreviation.

  2. The abbreviations for each state and their respective population, is a tab separated file – abbreviation(tab) integer.

You should read the first file into a HashMap that can handle the types. and then you should read the second file into a HashMap that can handle those types.

You should create a menu driven system that requests either the name or the abbreviation for a state. If the user enters a name of the state, your program should find the abbreviation for that state (using the first HashMap) and then call the second HashMap to find the population.

or if the user enters the abbreviation, your program should look up the population directly. Please display the results of the request in a clear and concise manner. If the user simply requests the population of the United States you may use the second HashMap and sum the values for all of the states and display that directly.

i created a menu and then created 2 hash maps but i don't know how to get the user input to search only for one key. At this point my hash map reads everything from the file instead of just what the user enters.

Print Menu:

public class Main {


    //public static void main(String[] args) {

        public static int menu() {

            int selection;
            Scanner input = new Scanner(System.in);

            /***************************************************/

            System.out.println("                Choose from these choices                          ");
            System.out.println("                -------------------------\n");
            System.out.println("1 - Type 1 to Enter an Abbreviation of the state you would like the population for. ");
            System.out.println("2 - Type 2 to Enter Name of the State you would like the population for. ");
            System.out.println("3 - Type 3 to find the population for whole United States(Census 2010). ");
            System.out.println("4 - Type 4 to Exit the Program.");

            selection = input.nextInt();
            return selection;    
        }

    //Once you have the method complete you would display it accordingly in your main method as follows:

        public static void main(String[] args) {

            int userChoice;
            Scanner input = new Scanner(System.in);

            /*********************************************************/

            userChoice = menu();

            //from here I can either use a switch statement on the userchoice 
            //or use a while loop (while userChoice != the fourth selection)
            //using if/else statements to do my actual functions for my choices.
        }
}

First Hash Map

import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;


    public class hash1 {


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

            File fi1 = new File("StatePlusAbbrev.txt");
            Scanner sc1 = new Scanner(fi1).useDelimiter("[,\n\r]+");
            HashMap<String, String> states = new HashMap<>();
            // HashMap<String, Integer> abbrevToPop = new HashMap<>();
            String stateName;
            String stateAbbrev;

            while (sc1.hasNextLine()) {
                stateName = sc1.next();
                stateAbbrev = sc1.next();
                states.put(stateName, stateAbbrev);
         //System.out.println("The abbreviation for " +stateName + " is " +
                // stateAbbrev);
            }

            // Set<Entry<String,String>> pod = states.entrySet();

            // iterate through the key set and display key and values
             Set<String> keys = states.keySet();
         for (String key : keys) {

//          
         System.out.print("Key = " + key);

         System.out.println("  Value = " + states.get(key) +"\n");
    //  
             }

            //Set set = states.entrySet();

            // check set values
            //System.out.println("Set values: " + set);

//           states.forEach((k, v) ->
//           System.out.println(k + "=\t" + v));
//          
    //
//          String out = states.get("New York");
//          System.out.println(out);





        }

        /**
         * 
         */
        public void menu() {


        }

    }

Any help would be appreciated. the second hash map is very similar except it has the string and int to read the abbreviations and number of population

You are supposed to read everything in once at the start of the program. Once it's all in the hash maps you can use their get methods to search them and retrieve the requested results for as many requests as you want.

Well how do I get the program to only find what the user enters. For example if the user enter al it only show Alabama and its population and not the whole file for all states...

I already told you. Use the get method for the appropriate map.
Eg
After you have loaded the abbreviations and populations into map2, to get the pop for NY, map2.get("NY");

This article has been dead for over six months. Start a new discussion instead.