我有两个文件,正在尝试使用扫描仪逐行读取每个文件。另外,我想通过使用多重映射将这两个文件与相同的Key(name)合并,以将这两个文件合并为一个。这是我到目前为止的脚本。有人可以给我建议吗?谢谢。

001.csv包含:

David 188 Male doctor A
Jacob 190 Male CEO    A+
Sam   175 Male Engineer A-


002.txt包含:

David 80kg US3000
Jacob 70kg US100000
Sam   65kg US80000


源代码:

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) );
        }
    }
}

最佳答案

请分享您面临的问题或得到的输出。
我为您运行代码,它对我有用。
我使用了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) );
        }
    }


输出

David   [188 Male doctor A  , 80kg US3000  ]
Jacob   [190 Male CEO    A+  , 70kg US100000  ]
Sam [  175 Male Engineer A-  ,   65kg US80000   ]

关于java - 如何结合扫描仪和多图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23806825/

10-10 07:07