Reputation: 3
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;
}
}
Upvotes: 0
Views: 1896
Reputation: 393781
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;
Upvotes: 2