11/13 Hash Maps


#1



I assume there is something wrong with the final line but I honestly do not know what it is as it won't print out.


import java.util.HashMap;

public class Restaurant {
	public static void main(String[] args) {
		HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
	
    restaurantMenu.put("Turkey Burger", 13);
    restaurantMenu.put("Naan Pizza", 11);
    restaurantMenu.put("Cranberry Kale Salad", 10);
	}  
System.out.println(restaurantMenu.get("Naan Pizza") );
}.


#2

Your code does not compile, therefore it doesn't run and and such prints nothing.

What you want to do when you get multiple errors is to read from the top, fix that one thing, and then re-compile before looking at the rest of them.

$ cat > Restaurant.java  # write your code to file
import java.util.HashMap;

public class Restaurant {
        public static void main(String[] args) {
                HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();

    restaurantMenu.put("Turkey Burger", 13);
    restaurantMenu.put("Naan Pizza", 11);
    restaurantMenu.put("Cranberry Kale Salad", 10);
        }  
System.out.println(restaurantMenu.get("Naan Pizza") );
}
$ javac Restaurant.java  # compile
Restaurant.java:11: error: <identifier> expected
System.out.println(restaurantMenu.get("Naan Pizza") );
                  ^
Restaurant.java:11: error: <identifier> expected
System.out.println(restaurantMenu.get("Naan Pizza") );
                                     ^
Restaurant.java:11: error: ';' expected
System.out.println(restaurantMenu.get("Naan Pizza") );
                                      ^
Restaurant.java:11: error: illegal start of type
System.out.println(restaurantMenu.get("Naan Pizza") );
                                                  ^
Restaurant.java:11: error: <identifier> expected
System.out.println(restaurantMenu.get("Naan Pizza") );
                                                   ^
5 errors

#3

look closely where your PrintLn function is located


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.