在 Java 中将 .csv 文件中的标记添加到 ArrayList
我在使用从 .csv 文件获取的标记在 Java 中创建 ArrayList
时遇到了问题。我已经尝试了几个小时但没有成功。如果不创建 ArrayList,我的所有令牌都会毫无问题地打印出来,但是当我创建一个 ArrayList 将它们添加到其中时,我遇到了麻烦。对于由 60 多行和每行 9 个标记(字符串)组成的 .csv 文件,我在添加到 ArrayList 之前打印出我所拥有的内容,但在创建 ArrayList 并向其中添加标记之后,在遇到 NoSuchElementException
之前,我只能打印出前几个标记。
这是我的全部代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
import java.lang.String;
import java.util.ArrayList;
import java.util.*; //probably redundant
import java.util.Scanner;
public class TTcompiler18Feb {
private static ArrayList<String> list;
public static void main(String[] args) {
try
{
//csv file containing data
String strFile = "FileWithTokens.csv";
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader( new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
list = new ArrayList<String>();
String token;
//read comma separated file line by line
while( (strLine = br.readLine()) != null)
{
lineNumber++;
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens())
{
tokenNumber++;
System.out.println("Line # " +
lineNumber + ", Token # " +
tokenNumber + ", Token : "+
st.nextToken());
list.add(st.nextToken());
}
//reset token number
tokenNumber = 0;
}
System.out.println("The size of your list is: " + list.size());
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);
}
}
}
我运行我的代码并得到以下结果:
运行 TTcompiler18Feb
Line # 1, Token # 1, Token : Row1Token1
Line # 1, Token # 2, Token : Row1Token2
Line # 1, Token # 3, Token : Row1Token3
Line # 1, Token # 4, Token : Row1Token4
Line # 1, Token # 5, Token : Row1Token5
Exception while reading csv file: java.util.NoSuchElementException
除了 list.add(st.nextToken());
之外,我还尝试以其他方式添加到我的列表中,例如使用 .toString
方法、类型转换 st.nextToken()
,并将变量分配给 st.nextToken()
,如下所示:
token = st.nextToken().toString(); //defined as string type outside loop
list.add(token);
...但这些方法似乎都不起作用。
该程序的目标是获取传入 .csv 文件中的所有标记,并将它们整齐地放入 ArrayList
中以供稍后使用。我对 Java 还很陌生,并且仍然不清楚泛型如何在解决这个问题中发挥作用。据我了解,StringTokenizer
是“字符串”类型,但我需要使用
将我的令牌输入 ArrayList
由
组成,但不知道如何组成。非常感谢任何帮助/提示/反馈!
I've been having trouble creating an ArrayList
in Java from tokens that I get from a .csv file. I've been trying to figure it out for hours with no success. Without creating an ArrayList
, all my tokens print out without a problem, but when I create an ArrayList
to add them to, I run into trouble. For a .csv file consisting of 60+ lines and 9 tokens (strings) for each line, I print out what I have before adding to the ArrayList
, but after creating the ArrayList and adding tokens to them, I can only print out the first few tokens before I run into a NoSuchElementException
.
This is all my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
import java.lang.String;
import java.util.ArrayList;
import java.util.*; //probably redundant
import java.util.Scanner;
public class TTcompiler18Feb {
private static ArrayList<String> list;
public static void main(String[] args) {
try
{
//csv file containing data
String strFile = "FileWithTokens.csv";
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader( new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
list = new ArrayList<String>();
String token;
//read comma separated file line by line
while( (strLine = br.readLine()) != null)
{
lineNumber++;
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens())
{
tokenNumber++;
System.out.println("Line # " +
lineNumber + ", Token # " +
tokenNumber + ", Token : "+
st.nextToken());
list.add(st.nextToken());
}
//reset token number
tokenNumber = 0;
}
System.out.println("The size of your list is: " + list.size());
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);
}
}
}
I run my code and get the following:
run TTcompiler18Feb
Line # 1, Token # 1, Token : Row1Token1
Line # 1, Token # 2, Token : Row1Token2
Line # 1, Token # 3, Token : Row1Token3
Line # 1, Token # 4, Token : Row1Token4
Line # 1, Token # 5, Token : Row1Token5
Exception while reading csv file: java.util.NoSuchElementException
I've tried adding to my list in other ways besides list.add(st.nextToken());
, such using a .toString
method, typecasting st.nextToken()
, and assigning a variable to st.nextToken()
, like so:
token = st.nextToken().toString(); //defined as string type outside loop
list.add(token);
...but none of these approaches seem to work.
The goal of this program is to take all the tokens in the incoming .csv file and neatly put them in an ArrayList
to be used later. I'm still pretty new to Java, and am still unclear as to how generics may play a role in solving this problem. As I understand it, StringTokenizer
is of type 'string', but I need to use a <String>
to enter my tokens into an ArrayList
composed of <Strings>
, but not sure how to. Any help/hints/feedback are much appreciated!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
A) 每次循环时都会调用
st.nextToken()
两次。你刚刚丢失了所有其他令牌,最终它呕吐了,因为在第二次调用时......没有一个(如果你的 csv 文件中每行有 10 个项目,它会成功,并且你的 < code>ArrayList)B) 不要在新代码中使用
StringTokenizer
。它的 Javadoc 解释说它是一个遗留类并使用String.split()
代替。完毕。
编辑添加:如果您确实需要
ArrayList
而不是数组,那么您可以执行以下操作:A) You're calling
st.nextToken()
twice each time you loop. You just lost every other token, and eventually it pukes because on the second call ... there isn't one (If you had 10 items per line in your csv file, it would succeed and you'd have 5 things in yourArrayList
)B) Don't use
StringTokenizer
in new code. The Javadoc for it explains it's a legacy class and to useString.split()
instead.Done.
Edit to add: if you really need an
ArrayList
rather than an array, you can then do:每次循环时,您都会向前跳跃两个元素。代码读取令牌然后使用它:
然后:
you are jumping two elements ahead each time you loop. Code read token and then use it:
And then:
nextToken 方法在循环中被调用两次,您可能需要将代码更改为:
the nextToken method is called twice in the loop, you may need to change your code to something like:
正如其他答案中提到的,您的问题是在一次迭代中多次调用
nextToken
,这不是它的预期使用方式。为什么不使用
String.split()
而不是使用笨重的旧StringTokenizer
呢?As mentioned in other answers, your problem is with calling
nextToken
more than once in a single iteration, which is not how it's intended for use.Instead of using the clunky old
StringTokenizer
, why don't you useString.split()
?您已经在
System.out
中执行了st.nextToken
。那么这个token就不再存在了。如果您在System.out
中需要它,请先将st.nextToken
分配给变量,然后再将其分配给变量。然后使用它。You are already doing an
st.nextToken
in yourSystem.out
. So the token won't exist anymore. If you need it inSystem.out
, assign thest.nextToken
to a variable first & then use it.