0

Could you please help me with following code ? I've created a class with static method which should return variable with type ArrayList<ArrayList<String>>. But I got an error in the return statement in the end of the function run(). It says that data cannot be resolved to a variable...

    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;
      }
}
3
  • Where is the declaration of data variable? Commented Jul 26, 2015 at 12:52
  • 1
    You have declared the data variable in try block and using it outside it Commented Jul 26, 2015 at 12:53
  • define data variable out of try block Commented Jul 26, 2015 at 12:53

1 Answer 1

2

data is declared inside the try block, so it's not in scope after it. Move its declaration to be before the try block.

    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;
Sign up to request clarification or add additional context in comments.

1 Comment

Using try-with-resources would simplify the method further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.