2014-09-05 45 views
1

所以我有這個代碼到目前爲止,我希望掃描儀從文件中讀取,我想第二個數組增加每次從第一個數組重複一個單詞......但它會搞砸對於第一類....任何想法?從文本文件讀取並行數組

Scanner sc=new Scanner(new File("Categories.txt")); 
    String category=sc.nextLine(); 
    int catnum=sc.nextInt(); 
    int count[]=new int[catnum]; 
    ArrayList<String>anim=new ArrayList<String>(); 
    while(sc.hasNextLine()) 
    { 
     String temp=sc.nextLine(); 
     anim.add(temp); 
    } 

    Collections.sort(anim); 
    System.out.println(anim); 

    for(int i=0;i<count.length;i++) 
    { 
     String x=anim.get(0); 
     for(int j=0;j<anim.size();j++) 
     { 
      if(anim.get(j).equals(x)) 
      { 
       count[i]++; 
       anim.remove(j--); 
      } 

     } 
    } 
    for(int i=0;i<count.length;i++) 
    { 
     System.out.println(count[i]); 
    } 

回答

0

count.size()返回0,因爲你永遠不添加任何東西,所以你永遠在循環進入第一次。改爲使用scategories.size()

+0

我實際上改變了它......現在請你看看它嗎? – user3196301 2014-09-05 01:48:31

+0

您完全更改了代碼...您不清楚您嘗試實現的目標。 – Dici 2014-09-05 14:31:41