-2

我正在尝试从 txt 文件中打印出索引,并且我正在使用扫描仪读取下面的文件我似乎在将单词放入数组列表中时遇到了问题

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

1 回答 1

0

This code cannot compile.

You are trying to put an integer in a Map<String, ArrayList<String>>.

This is the line:

concordance.put(key, 1))

Also this line fails:

value++;

Since value is a collection of Integer, not an integer.

This line will always return false at runtime:

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

Because integer is declared as ArrayList, and you are searching a collection of Integer.

In general, you have a great confusion between Integer and ArrayList<Integer>. You are also making calls to scan.nextLine() without noticing that it ADVANCES a line each time it is called.

于 2011-11-10T09:31:04.307 回答