gpt4 book ai didi

java - 变量无法解析为变量

转载 作者:行者123 更新时间:2023-12-02 04:29:40 26 4
gpt4 key购买 nike

您能帮我编写以下代码吗?我创建了一个类 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;
}
}

最佳答案

data 在 try block 内声明,因此不在其之后的范围内。将其声明移至 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;

关于java - 变量无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31637156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com