/** * Title: HashMapPlay * Description: Simple example of using HashMap * @author hollingd@cs.rpi.edu */ import java.util.*; import java.io.*; // define out own type of exception class HashMapPlayParseException extends Exception { HashMapPlayParseException() { super(); } HashMapPlayParseException(String s) { super(s); } } class HashMapPlay { Map text_to_int; // main creates a HashMapPlay object and calls doIt public static void main(String [] args ) { HashMapPlay p = new HashMapPlay(); p.doIt(); } // default constructor creates a HashMap public HashMapPlay() { initialize_map(); } // doIt reads lines from stdin, and calls handleLine for each one. /*public void doIt() { while (true) { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); // read until EOF (readLine returns a NULL reference) while (s != null) { handleLine(s); s = in.readLine(); } } catch (Exception e) { // e.printStackTrace(); } } } */ // A better test method - this one runs through all lines of // input instead of stopping at the first problem. public void doIt() { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); while (s != null) { try { handleLine(s); } catch (HashMapPlayParseException he) { System.out.println("Error: " + he.getMessage()); } s = in.readLine(); } } catch (Exception e) { e.printStackTrace(); } } /** hashmap text_to_int associates * strings with integer numeric values (for some integer values) * * Since both the key and value must be objects * (not primitive types), we need to have the values * be the Integer wrapper class. */ public void initialize_map() { // Create a HashMap object text_to_int = new HashMap(); // populate the map text_to_int.put("zero", new Integer(0)); text_to_int.put("one", new Integer(1)); text_to_int.put("two", new Integer(2)); text_to_int.put("three",new Integer(3)); text_to_int.put("four", new Integer(4)); text_to_int.put("five", new Integer(5)); text_to_int.put("six", new Integer(6)); text_to_int.put("seven",new Integer(7)); text_to_int.put("eight",new Integer(8)); text_to_int.put("nine", new Integer(9)); text_to_int.put("ten", new Integer(10)); } /** HandleLine takes a string and attempts to parse it and compute the answer. The valid expressions contain a number (specified as an english word in our map) followed by "plus" or "minus" followed by another number in english. Throws an exception if anything goes wrong, otherwise prints out the answer (the value of the expression). */ public void handleLine(String input) throws Exception { int op1,op2; int answer; String operator; // parse the string - we expect 3 tokens String tokens[] = input.split("\\s"); // convert the first token to an int if (text_to_int.containsKey(tokens[0])) { op1 = ((Integer) text_to_int.get(tokens[0])).intValue(); } else { throw new HashMapPlayParseException("first operand not valid"); } // convert the second token to an int if (text_to_int.containsKey(tokens[2])) { op2 = ((Integer) text_to_int.get(tokens[2])).intValue(); } else { throw new HashMapPlayParseException("second operand not valid"); } // make sure the operation (middle token) is valid // if so - compute the answer if (tokens[1].equals("plus")) { answer = op1 + op2; operator = "+"; } else if (tokens[1].equals("minus")) { answer = op1 - op2; operator = "-"; } else { throw new HashMapPlayParseException("invalid operator"); } System.out.println(op1 + " " + operator + " " + op2 + " = " + answer); } }