初始化 ArrayList 时出错
public class driver
{
private static ArrayList<String> arrayList ;
TownBankRecord TBR = new TownBankRecord();
ArrayList<Record> TBList = new ArrayList<Record>();
public void getDate()
{
try
{
BufferedReader br = new BufferedReader (new FileReader ("data/bank-data.csv"));
String line= " ";
int tokenCount=0;
//BankRecord bank= new BankRecord();
while((line=br.readLine())!=null)
{
if (!line.equals(","))
{
StringTokenizer st = new StringTokenizer(line,",");
while (st.hasMoreTokens())
{
arrayList.add(st.nextToken());
tokenCount++;
}
}
if (tokenCount==11)
{
er = new TownBankRecord(arrayList);
TBList.add(er);
tokenCount=0;
}
}
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
catch (IOException e)
{
System.err.println("Caught IOException: "
+ e.getMessage());
}
}
}
上面的代码读取 CSV 文件。我使用了一个标记生成器,它从每一行获取标记,然后将它们传输到字符串的 ArrayList 中。然后 ArrayList 被传输到 TownBankRecord 的对象er
,然后最终传输到 TBlist(记录数据类型)。我有以下错误:
cannot find symbol
symbol: variable er
location: class nidhin.driver.
有什么想法吗?
public class driver
{
private static ArrayList<String> arrayList ;
TownBankRecord TBR = new TownBankRecord();
ArrayList<Record> TBList = new ArrayList<Record>();
public void getDate()
{
try
{
BufferedReader br = new BufferedReader (new FileReader ("data/bank-data.csv"));
String line= " ";
int tokenCount=0;
//BankRecord bank= new BankRecord();
while((line=br.readLine())!=null)
{
if (!line.equals(","))
{
StringTokenizer st = new StringTokenizer(line,",");
while (st.hasMoreTokens())
{
arrayList.add(st.nextToken());
tokenCount++;
}
}
if (tokenCount==11)
{
er = new TownBankRecord(arrayList);
TBList.add(er);
tokenCount=0;
}
}
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
catch (IOException e)
{
System.err.println("Caught IOException: "
+ e.getMessage());
}
}
}
The above code reads from a CSV file. I have used a tokenizer which gets the tokens from each line and then transfers them to an ArrayList of Strings. The ArrayList is then transfered to an object er
of TownBankRecord which then finally transfered to TBlist (Record datatype). I have the following error for er:
cannot find symbol
symbol: variable er
location: class nidhin.driver.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你永远不会将
er
声明为变量。您也不使用您声明的类字段TBR
。这些是相同的吗?You never declare
er
as a variable. You also don't use the class field you do declare,TBR
. Are these intended to be the same?更改:
改为:
Change:
to this:
看起来您还没有在任何地方声明
er
。您可以在类中将er
声明为成员。您还可以在getDate()
方法中声明er
,只需确保在使用它之前初始化er
即可。这完全取决于您的要求。我可以看到您已将 arrayList 设为静态。如果您一次又一次地读取同一个文件,则无需将 arrayList 设为静态变量,最佳实践是在方法中声明它而不是作为类成员。Looks like you have not declared
er
anywhere. You can declareer
in the class as a member. You can also declareer
in the methodgetDate()
, just make sure you initializeer
before using it. It completely depends upon your requirements. I can see that you have madearrayList
static. If you are reading the same file again and again then there is no need to makearrayList
a static variable and the best practice is to declare it in method rather than as class member.