2017-11-06 3 views
0

デリミタを使用して、2つの異なるファイルを読み込みます.1つはコンマ区切り、もう1つはタブ区切りです。これらのファイルを2つの配列リストに入れます。今はどの国が失われた人口(私が信じているミシガンであるべきか)とどの人が得たのかを話して比較したいと思います。人口が失われたのか、増えたのかはどうすれば分かりますか?

package delim; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class MyMain { 

    public static void main(String[] args) throws IOException { 
     File fi = new File("2010census.txt"); 
     File fil = new File("2000census.txt"); 
     Scanner sc = new Scanner(fi).useDelimiter("[|,|\n|\r]+"); 
     Scanner kb = new Scanner(fil).useDelimiter("[\t|\n|\r]+"); 
     String stateName; 
     int numb; 
     ArrayList<Integer> arrL = new ArrayList<Integer>(); 
     ArrayList<Integer> arrL2 = new ArrayList<Integer>(); 

     //while loop to read 2000 census 
     while (kb.hasNext()) { 
      stateName = kb.next(); 
      numb = kb.nextInt(); 
      System.out.println(stateName + " " + numb); 
      arrL.add(numb); 
     } 
     //while loop to read 2010 census 
     while (sc.hasNext()) { 
      stateName = sc.next(); 
      numb = sc.nextInt(); 
      System.out.println(stateName + " " + numb); 
      arrL2.add(numb); 
     } 
    } 
} 
+0

Apache Commonsのようなcsvリーダーを入手してください。 – Robert

答えて

0

状態を値にリンクするには、HashMapを使用します。あなたは、CSVのを読む前にハッシュマップを宣言したいとキーによっていずれかを反復処理次に、あなたのcsvファイルの

HashMap<String,Integer> hm2000 = new HashMap<String,Integer>(); 
HashMap<String,Integer> hm2010 = new HashMap<String,Integer>(); 

// Add to hashmap 
map.put("<state>", population); 

に読みながらmap.putは、ループ内にある、他のハッシュマップに

for(Map.Entry<Integer, Book> entry:hm2000.entrySet()){  
    int key=entry.getKey(); 
    // Compare the value from the other hashmap, store it in an array/
    // other hashmap for reporting at the end 
    ... 
} 
を比較します

これで、データで必要なことを行うことができます。

注:これは彼がこれを行う最もエレガントな方法ではありませんが、それはあなたのための良いスタートする必要があります。

関連する問題