1. Modify the \"Lookup\" program given in the textbook (program 4.4.1) to make a
ID: 3742397 • Letter: 1
Question
1. Modify the "Lookup" program given in the textbook (program 4.4.1) to make a program LookupMultipleNumber that prints out multiple values having the same key. Mo7.1] Notes: The user would specify the maximum number of values to be printed out as a command line argument after the file name. Store all such multiple values in a queue. Sample runs would be as follows (refer to input file amino.csv). java LookupMultipleNumber amino.csv 230 Tryptophan TGG Cysteine TGT TGC CTRL-D> Sjava Lookup MultipleNumber amino.csv 6 30 Cysteine TGT TGC Leucine TTA TTG CTT CTC CTA CTGExplanation / Answer
**If you need to add single value in a dictionary you will use the following
where first String is key and second string is value,
**Since we have add the multiple values, we can not add it directly, first we have to make the collection of the multiple values, for these we can use ArrayList of String
so step 1:- Create a ArrayList<String> val and add all the values in it.
step 2 :- Now put the key and value in the following dictionary
ST<String, ArrayList<String>> st = new ST<String, ArrayList<String>>();
Below is the program for the same, at one time only one key value pair can exist in a map, so to add multiple values make the collection of values and assign it.
------------------------------------------------LookupMultipleNumber.java---------------------------------------------------
import java.util.ArrayList;
public class LookupMultipleNumber {
// Do not instantiate.
private LookupCSV() { }
public static void main(String[] args) {
int keyField = Integer.parseInt(args[1]);
int valField1 = Integer.parseInt(args[2]);
int valField2 = Integer.parseInt(args[3]);
// symbol table
ST<String, ArrayList<String>> st = new ST<String, ArrayList<String>>();
// read in the data from csv file
In in = new In(args[0]);
while (in.hasNextLine()) {
String line = in.readLine();
String[] tokens = line.split(",");
String key = tokens[keyField];
String val1 = tokens[valField1];
String val2 = tokens[valField2];
ArrayList<String> val = new ArrayList<String>();
val.add(val1);
val.add(val2);
st.put(key, val);
}
while (!StdIn.isEmpty()) {
String s = StdIn.readString();
if (st.contains(s)) StdOut.println(st.get(s));
else StdOut.println("Not found");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.