Sunday, August 25, 2013

scanner with hashmap

import java.util.*;
import java.util.Map.Entry;

public class ExampleWithScannerAndHashMap {
                public static void main(String[] args) {
                                int count = 0;// stores the number of names
                                // Scanner the reads the user input from standard input stream
                                Scanner in = new Scanner(System.in);
                                // Request user input till getting a valid number
                                System.out.println("Enter the Number of Names U wants to display :");
                                while (true) {
                                                try {
                                                                // Read the user input and convert into integer
                                                                count = in.nextInt();
                                                                // If it is a valid number break the loop
                                                                break;
                                                } catch (InputMismatchException e) {
                                                                // If it is not a valid number show the error msg
                                                                System.out.println("Please enter a valid number : ");
                                                }
                                }
                                in.nextLine(); // reads the enter char from buffer
                                // Hashmap to store the names
                                Map<String, String> hashMap = new HashMap<String, String>();
                                for (int i = 1; i <= count; i++) {
                                                System.out.println("Enter Name " + i + " : ");
                                                // Add the names to hashmap with different keys
                                                hashMap.put("name" + i, in.nextLine());
                                }
                                // Print the names in the hashmap
                                printValues(hashMap);
                                System.out.println("Enter a key to delete : ");
                                String key = in.nextLine();
                                if (hashMap.containsKey(key)) {
                                                hashMap.remove(key);
                                                System.out.println("The element with key " + key
                                                                                + " has been removed successfully");
                                } else {
                                                System.out.println("The entered key does not exists");
                                }
                                //Clears the hashmap
                                hashMap.clear();
                                // close the scanner to release the resource
                                in.close();
                }

                public static void printValues(Map<String, String> hashMap) {
                                Iterator<Entry<String, String>> iterator = hashMap.entrySet()
                                                                .iterator();
                                System.out.println("There are " + hashMap.size() + " elements in HashMap. They are ");
                                System.out.println("Key : Value");
                                while (iterator.hasNext()) {
                                                Entry<String,String> entry = iterator.next();
                                                System.out.println(entry.getKey() + " : "
                                                                                + entry.getValue());
                                }
                }
}

No comments:

Post a Comment