繁体   English   中英

变量无法解析为变量

[英]Variable cannot be resolved to a variable

您能帮我提供以下代码吗? 我用static方法创建了一个类,该类应return类型为ArrayList<ArrayList<String>>变量。 但是我在run()函数结尾的return语句中遇到了错误。 它说不能将数据解析为变量 ...

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

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM