列出来自 opencsv 的连续列表

发布于 2024-11-17 15:32:26 字数 2365 浏览 7 评论 0原文

好的。首先我意识到 readAll() 将标记化字符串保存在数组索引中,从 0 开始,直到它看到换行符(csv 文件的下一行)并从 0 重新开始。我希望将标记化字符串保存在连续数组中。

import au.com.bytecode.opencsv.CSVReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class ReadTest {


    private static String data0 = "U:\\test-csv1.csv";
    private static String [] longArray = new String[50];
    private static String[] aString;
    private static List<String> longStr = new java.util.ArrayList<String>();
    private static int arrayCtr = 0;

    private static  void copyArray(String aTemp){

        longStr.add(arrayCtr, aTemp);
        System.out.println(arrayCtr);
        arrayCtr++;
        }

    private static void printElements(){

        for(int no =15;no<= 25;no++)
        System.out.print("\nelNo "+no+" is: "+longStr.get(no));
        System.out.println();
    }

    public static void main(String[] args) throws IOException {


    CSVReader reader1 = new CSVReader(new FileReader(data0),';');
    List aList;



        while ((aList = reader1.readAll())!= null){
            int outer= 0;
            String aTemp;
            String [] longArray = new String[50];

所以在这里我做了一个循环来复制标记化的字符串,并将其复制到名为 longStr 的列表中。

            for (int counter= 0;counter <aList.size();counter++){
                String [] tempStr = (String[]) aList.get(counter);
                for (int j = 0; j < tempStr.length; j++){

                    aTemp = tempStr[j];
                    copyArray(aTemp);
                //  System.out.print(tempStr[j]);
                //  System.out.println();
                    System.out.print("strNo: " + j+"="+tempStr[j] + " "+"\n");
                    System.out.print("TEMP="+aTemp+"\n");


                }

这是一个获取行数的计数器。

                System.out.println("loop for no. of element:"+(++outer));
                System.out.println();


                        printElements();
            }

printElements 是一种列出 longStr 中元素的方法。

我在列出 longStr 中的所有元素时遇到问题。这就是我到目前为止所拥有的,我尝试将 readAll() 中的标记化字符串排列到连续数组中,如 longStr 中所示。当我尝试在 readAll() 完成读取 csv 文件后打印出元素时,它会连续打印 printElements() 中的元素,即不间断。我该如何解决这个问题,除了 while 内之外,我可以将 printElements() 放在哪里?如果我把它放在其他地方,我总是会收到像indexoutofbound这样的错误。

OK.first i realized that readAll() saved the tokenized String in array index from 0 until it sees the newline char(next row of a csv file) and start again from 0.I want the tokenized String saved in a continuous array.

import au.com.bytecode.opencsv.CSVReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class ReadTest {


    private static String data0 = "U:\\test-csv1.csv";
    private static String [] longArray = new String[50];
    private static String[] aString;
    private static List<String> longStr = new java.util.ArrayList<String>();
    private static int arrayCtr = 0;

    private static  void copyArray(String aTemp){

        longStr.add(arrayCtr, aTemp);
        System.out.println(arrayCtr);
        arrayCtr++;
        }

    private static void printElements(){

        for(int no =15;no<= 25;no++)
        System.out.print("\nelNo "+no+" is: "+longStr.get(no));
        System.out.println();
    }

    public static void main(String[] args) throws IOException {


    CSVReader reader1 = new CSVReader(new FileReader(data0),';');
    List aList;



        while ((aList = reader1.readAll())!= null){
            int outer= 0;
            String aTemp;
            String [] longArray = new String[50];

so here i make a loop to copy the tokenized String to be copied into a List called longStr.

            for (int counter= 0;counter <aList.size();counter++){
                String [] tempStr = (String[]) aList.get(counter);
                for (int j = 0; j < tempStr.length; j++){

                    aTemp = tempStr[j];
                    copyArray(aTemp);
                //  System.out.print(tempStr[j]);
                //  System.out.println();
                    System.out.print("strNo: " + j+"="+tempStr[j] + " "+"\n");
                    System.out.print("TEMP="+aTemp+"\n");


                }

this is a counter to obtain the num of row.

                System.out.println("loop for no. of element:"+(++outer));
                System.out.println();


                        printElements();
            }

printElements is a method to list down the elements in longStr.

I am having a problem in listing all the elements in longStr. This is what I have so far, I tried to arrange tokenized String from readAll() into continuous array as in longStr. When I tried to print out the elements after readAll() finished reading the csv file, it continuously print the elements from printElements() ie nonstop. How do I solve this, and where can I put the printElements() other than inside the while? I always get error like indexoutofbound if I place it somewhere else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浮华 2024-11-24 15:32:26

我认为

while ((aList = reader1.readAll())!= null){

是一个无限循环。尝试

if((aList = reader1.readAll())!= null){

I think

while ((aList = reader1.readAll())!= null){

is an endless loop. try

if((aList = reader1.readAll())!= null){
蓝海似她心 2024-11-24 15:32:26

为什么不直接调用reader.readAll(),然后迭代该列表呢?

List<String[]> records = reader.readAll();
for (String[] record : records) {
  //do something with each record
}

Why don't you just call reader.readAll() and then iterate over that List?

List<String[]> records = reader.readAll();
for (String[] record : records) {
  //do something with each record
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文