简体   繁体   中英

Converting CSV File to Java - Copied in Backwards

I previously asked a question about converting a CSV file to 2D array in java. 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. When reading the CSV file in, I saved it into a String ArrayList then I converted that into a single dimension int array. Any input would be appreciated. Thanks

so you want to read a CSV file in java , then you might wanna use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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