I have two files and am trying to read each file line by line by using scanner. Also, I would like to combine these two file with the same Key(name) by using multimap in order to combine these two file into one. Here is the script I have so far. Can someone please give me the suggestion? Thank you.
001.csv contains:
David 188 Male doctor A
Jacob 190 Male CEO A+
Sam 175 Male Engineer A-
002.txt contains:
David 80kg US3000
Jacob 70kg US100000
Sam 65kg US80000
Source code:
public class same_test{
public static void main (String[] args) throws FileNotFoundException {
MultiMap multiMap = new MultiValueMap();
Scanner scanner1 = new Scanner(new File("001.csv"));
Scanner scanner2 = new Scanner(new File("002.txt"));
while (scanner1.hasNextLine()) {
String line = scanner1.nextLine();
String[] array = line.split("\t",2);
String TheName = array[0];
String score = array[1];
multiMap.put(TheName,score);
}
while (scanner2.hasNextLine()) {
String line2 = scanner2.nextLine();
String[] array2 = line2.split("\t",2);
String TheName2 = array2[0];
String rs = array2[1];
multiMap.put(TheName2,rs);
}
Set<String> keys = multiMap.keyset();
for (String key : keys){
System.out.println(key + "\t" + multiMap.get(key) );
}
}
}
Plz share what problem you facing or output you getting. I run you code, it works for me. I used MultiHashMap.
public static void main (String[] args) throws FileNotFoundException {
MultiMap multiMap = new MultiHashMap();
Scanner scanner1 = new Scanner(new File("/obp/f1.csv"));
Scanner scanner2 = new Scanner(new File("/obp/f2.csv"));
while (scanner1.hasNextLine()) {
String line = scanner1.nextLine();
String[] array = line.split("\\s",2);
String TheName = array[0];
String score = array[1];
multiMap.put(TheName,score);
}
while (scanner2.hasNextLine()) {
String line2 = scanner2.nextLine();
String[] array2 = line2.split("\\s",2);
String TheName2 = array2[0];
String rs = array2[1];
multiMap.put(TheName2,rs);
}
Set<String> keys = multiMap.keySet();
for (String key : keys){
System.out.println(key + "\t" + multiMap.get(key) );
}
}
output
David [188 Male doctor A , 80kg US3000 ]
Jacob [190 Male CEO A+ , 70kg US100000 ]
Sam [ 175 Male Engineer A- , 65kg US80000 ]