简体   繁体   English

将CSV文件转换为Java-向后复制

[英]Converting CSV File to Java - Copied in Backwards

I previously asked a question about converting a CSV file to 2D array in java. 我之前曾问过一个有关在Java中将CSV文件转换为2D数组的问题。 I completely rewrote my code and it is almost reworking. 我完全重写了我的代码,它几乎在重新编写。 The only problem I am having now is that it is printing backwards. 我现在遇到的唯一问题是它向后打印。 In other words, the columns are printing where the rows should be and vice versa. 换句话说,列正在打印行应该在的位置,反之亦然。 Here is my code: 这是我的代码:

 int [][] board = new int [25][25];

     String line = null;
     BufferedReader stream = null;
     ArrayList <String> csvData = new ArrayList <String>();

     stream = new BufferedReader(new FileReader(fileName));
        while ((line = stream.readLine()) != null) {
            String[] splitted = line.split(",");
            ArrayList<String> dataLine = new ArrayList<String>(splitted.length);
            for (String data : splitted)
                dataLine.add(data);
            csvData.addAll(dataLine);

        }

        int [] number = new int [csvData.size()];

        for(int z = 0; z < csvData.size(); z++)
        {
            number[z] = Integer.parseInt(csvData.get(z));
        }

        for(int q = 0; q < number.length; q++)
        {
            System.out.println(number[q]);
        }

        for(int i = 0; i< number.length; i++)
        {
            System.out.println(number[i]);
        }



        for(int i=0; i<25;i++)
            {
               for(int j=0;j<25;j++)
               {
                   board[i][j] = number[(j*25) + i]; 

            }
            }

Basically, the 2D array is supposed to have 25 rows and 25 columns. 基本上,二维数组应该具有25行和25列。 When reading the CSV file in, I saved it into a String ArrayList then I converted that into a single dimension int array. 在读取CSV文件时,我将其保存到String ArrayList中,然后将其转换为一个单维int数组。 Any input would be appreciated. 任何输入将不胜感激。 Thanks 谢谢

so you want to read a CSV file in java , then you might wanna use OPEN CSV 因此,您想在java中读取CSV文件,那么您可能想使用OPEN CSV

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

import au.com.bytecode.opencsv.CSVReader;

public class CsvFileReader {
    public static void main(String[] args) {

        try {
            System.out.println("\n**** readLineByLineExample ****");
            String csvFilename = "C:/Users/hussain.a/Desktop/sample.csv";
            CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
            String[] col = null;
            while ((col = csvReader.readNext()) != null) 
            {
                System.out.println(col[0] );
                //System.out.println(col[0]);
            }
            csvReader.close();
        }
        catch(ArrayIndexOutOfBoundsException ae)
        {
            System.out.println(ae+" : error here");
        }catch (FileNotFoundException e) 
        {
            System.out.println("asd");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("");
            e.printStackTrace();
        }
    }
}

and you can get the related jar file from here 您可以从此处获取相关的jar文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM