2011-11-10 31 views
-2

我想用扫描仪读取文件下我似乎有把词语的数组列表一致性树形图的Java

public class Concordance 
{ 
    public static void main (String[]args) throws IOException 
    { 
     TreeMap <String, ArrayList<Integer>> concordance = new TreeMap <String, ArrayList<Integer>>(); 
     File myfile = new File ("Caesar.txt"); 
     Scanner scan = new Scanner(myfile); 
     ArrayList <Integer > integer = new ArrayList <Integer>(); 
     for (int i = 0; i < scan.nextLine().length(); i++) 
     { 
      String key = scan.nextLine().toLowerCase(); 
      if (scan.nextLine().length(i) > 1) 
      { 
       if (concordance.get(key) == null) { 
        concordance.put(key, 1)) 
       } else { 
        ArrayList<Integer> value = concordance.get(key).indexOf(integer); 
        value++; 
        concordance.put(key, value); 
       } 
      } 
     } 
     System.out.println(concordance); 
    } 
} 
+0

有两个问题,第一个问题的解决方法是这里 http://stackoverflow.com/faq 解决了第一个问题之后,这里有人可能会解决第二个问题......干杯! – Zohaib

+0

'value ++'应该做什么? –

回答

0

这是一个问题,打印出从一个txt文件和IM的一致性代码无法编译。

您正试图在Map<String, ArrayList<String>>中输入一个整数。

这是行:

concordance.put(key, 1)) 

同样在此行失败:

value++; 

由于值是Integer的集合,而不是一个整数。

这条线将始终在运行时返回false:

ArrayList<Integer> value = concordance.get(key).indexOf(integer); 

因为integer被声明为ArrayList,而你正在寻找整数的集合。

一般而言,您在IntegerArrayList<Integer>之间存在很大的混淆。您也正在拨打电话scan.nextLine(),但不会注意到它每次调用时都会前进一行。