在 Java 中将 .csv 文件中的标记添加到 ArrayList

发布于 2025-01-06 19:33:18 字数 2840 浏览 1 评论 0原文

我在使用从 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

剪不断理还乱 2025-01-13 19:33:18

A) 每次循环时都会调用 st.nextToken() 两次。你刚刚丢失了所有其他令牌,最终它呕吐了,因为在第二次调用时......没有一个(如果你的 csv 文件中每行有 10 个项目,它会成功,并且你的 < code>ArrayList)

B) 不要在新代码中使用StringTokenizer。它的 Javadoc 解释说它是一个遗留类并使用 String.split() 代替。

String[] elements = strLine.split(",");

完毕。

编辑添加:如果您确实需要ArrayList而不是数组,那么您可以执行以下操作:

ArrayList<String> list = new ArrayList<String>(Arrays.asList(elements));

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 your ArrayList)

B) Don't use StringTokenizer in new code. The Javadoc for it explains it's a legacy class and to use String.split() instead.

String[] elements = strLine.split(",");

Done.

Edit to add: if you really need an ArrayList rather than an array, you can then do:

ArrayList<String> list = new ArrayList<String>(Arrays.asList(elements));
赠我空喜 2025-01-13 19:33:18

每次循环时,您都会向前跳跃两个元素。代码读取令牌然后使用它:

String token = ""; 
token = st.nextToken();

然后:

                            System.out.println("Line # " + 
                                               lineNumber + ", Token # " + 
                                               tokenNumber + ", Token : "+ 
                                               token ); 

                            list.add(token);

you are jumping two elements ahead each time you loop. Code read token and then use it:

String token = ""; 
token = st.nextToken();

And then:

                            System.out.println("Line # " + 
                                               lineNumber + ", Token # " + 
                                               tokenNumber + ", Token : "+ 
                                               token ); 

                            list.add(token);
冬天旳寂寞 2025-01-13 19:33:18

nextToken 方法在循环中被调用两次,您可能需要将代码更改为:

while(st.hasMoreTokens())
                    {
                            tokenNumber++;       
                            String tempStr = st.nextToken();
                            System.out.println("Line # " + 
                                               lineNumber + ", Token # " + 
                                               tokenNumber + ", Token : "+ 
                                               tempStr); 

                            list.add(tempStr);
                    }

the nextToken method is called twice in the loop, you may need to change your code to something like:

while(st.hasMoreTokens())
                    {
                            tokenNumber++;       
                            String tempStr = st.nextToken();
                            System.out.println("Line # " + 
                                               lineNumber + ", Token # " + 
                                               tokenNumber + ", Token : "+ 
                                               tempStr); 

                            list.add(tempStr);
                    }
鹿! 2025-01-13 19:33:18

正如其他答案中提到的,您的问题是在一次迭代中多次调用 nextToken ,这不是它的预期使用方式。

为什么不使用 String.split() 而不是使用笨重的旧 StringTokenizer 呢?

String[] tokens = strLine.split(",");

for(int i = 0; i < tokens.length; i++) { 
    System.out.println("Line # " + 
                        lineNumber + ", Token # " + 
                        i+ ", Token : "+ 
                        token[i]);       
    // do you even need a list?
    list.add(token[i]);
}

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 use String.split() ?

String[] tokens = strLine.split(",");

for(int i = 0; i < tokens.length; i++) { 
    System.out.println("Line # " + 
                        lineNumber + ", Token # " + 
                        i+ ", Token : "+ 
                        token[i]);       
    // do you even need a list?
    list.add(token[i]);
}
东北女汉子 2025-01-13 19:33:18

您已经在 System.out 中执行了 st.nextToken。那么这个token就不再存在了。如果您在 System.out 中需要它,请先将 st.nextToken 分配给变量,然后再将其分配给变量。然后使用它。

You are already doing an st.nextToken in your System.out. So the token won't exist anymore. If you need it in System.out, assign the st.nextToken to a variable first & then use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文