001package algs35;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac LookupCSV.java
005 *  Execution:    java LookupCSV file.csv keyField valField
006 *  Dependencies: ST.java In.java StdIn.java StdOut.java
007 *  Data files:   http://algs4.cs.princeton.edu/35applications/DJIA.csv
008 *                http://algs4.cs.princeton.edu/35applications/UPC.csv
009 *                http://algs4.cs.princeton.edu/35applications/amino.csv
010 *                http://algs4.cs.princeton.edu/35applications/elements.csv
011 *                http://algs4.cs.princeton.edu/35applications/ip.csv
012 *                http://algs4.cs.princeton.edu/35applications/morse.csv
013 *
014 *  Reads in a set of key-value pairs from a two-column CSV file
015 *  specified on the command line; then, reads in keys from standard
016 *  input and prints out corresponding values.
017 *
018 *  % java LookupCSV amino.csv 0 3     % java LookupCSV ip.csv 0 1
019 *  TTA                                www.google.com
020 *  Leucine                            216.239.41.99
021 *  ABC
022 *  Not found                          % java LookupCSV ip.csv 1 0
023 *  TCT                                216.239.41.99
024 *  Serine                             www.google.com
025 *
026 *  % java LookupCSV amino.csv 3 0     % java LookupCSV DJIA.csv 0 1
027 *  Glycine                            29-Oct-29
028 *  GGG                                252.38
029 *                                     20-Oct-87
030 *                                     1738.74
031 *
032 *
033 *************************************************************************/
034
035public class LookupCSV {
036        public static void main(String[] args) {
037                args = new String[] { "data/ip.csv", "0", "1" }; StdIn.fromString ("www.google.com");
038                //        args = new String[] { "data/ip.csv", "1", "0" }; StdIn.fromString ("216.239.41.99");
039                //        args = new String[] { "data/amino.csv", "0", "3" }; StdIn.fromString ("TTA");
040
041                int keyField = Integer.parseInt(args[1]);
042                int valField = Integer.parseInt(args[2]);
043
044                // symbol table
045                ST<String, String> st = new ST<>();
046
047                // read in the data from csv file
048                In in = new In(args[0]);
049                while (in.hasNextLine()) {
050                        String line = in.readLine();
051                        String[] tokens = line.split(",");
052                        String key = tokens[keyField];
053                        String val = tokens[valField];
054                        st.put(key, val);
055                }
056
057                while (!StdIn.isEmpty()) {
058                        String s = StdIn.readString();
059                        if (st.contains(s)) StdOut.println(st.get(s));
060                        else                StdOut.println("Not found");
061                }
062        }
063}