变量无法解析为变量

时间:2015-07-26 12:50:32

标签: java

你能帮我解决下面的代码吗?我已经使用static方法创建了一个类,return变量的类型为ArrayList<ArrayList<String>>。但是我在函数return末尾的run()语句中出错了。它说数据无法解析为变量 ...

    package com.ita;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public  class ReadCSV {

    public static  ArrayList<ArrayList<String>> run() {

        String csvFile = "RSS_usernames.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            ArrayList<String> RSSname = new ArrayList<String>();
            ArrayList<String> username= new ArrayList<String>();

             ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
            int i=0;

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {

                    // use comma as separator
                String[] country = line.split(cvsSplitBy);

                System.out.println("Country [code= " + country[0] 
                                     + " , name=" + country[1] + "]");

                RSSname.add(new String(country[0]));
                username.add(new String(country[1].trim()));

                 data.add(new ArrayList<String>());
                 data.get(i).add(country[0]);
                 data.get(i).add(country[1].trim());

                i=i+1;
            }

            System.out.println(data.get(0).get(1));



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("Done");

        return data;
      }
}

1 个答案:

答案 0 :(得分:2)

data在try块中声明,因此它不在范围之后。将其声明移到try块之前。

    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
    try {
        ArrayList<String> RSSname = new ArrayList<String>();
        ArrayList<String> username= new ArrayList<String>();
        ...
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");

    return data;