Home > Blockchain >  How can I assign each column from a csv file to an int number?
How can I assign each column from a csv file to an int number?

Time:07-19

Newbie here!

I have learnt how to read a csv file and display in into the console, but this time I need to assign each column of the csv file into an int number and return the method so that when I type number (1) for example, the first column of this file will be displayed to the screen.

 public double runAggregator(int i) throws IOException {    

    StockFileReader stReader = new StockFileReader (file);
   
    List<String> Column = st.readFileData();

CodePudding user response:

Each data line of the CSV file is most likely comma (,) delimited so when you read in each line, split it then display the column you want, for example:

public static void readAndDisplayDataColumn(String csvFilePath, int literalDataColumnToDisplay) throws IOException {
    if (throws literalDataColumnToDisplay < 1) {
         System.err.println("readAndDisplayDataColumn() method Error! "   
                    "Invalid column number ("   literalDataColumnToDisplay   
                    ") supplied!");
        return;
    }

    try (BufferedReader reader = new BufferedReader(new FileReader(csvFilePath))) {
        String line;
        while ((line = reader.readLine()) != null) {
            String[] columnData = line.split("\\s*,\\s*);
            System.out.println(columnData[literalDataColumnToDisplay - 1]);
        }
    }
}

Will display column 3 of each data line in file.

You will of notice that 1 is subtracted from literalDataColumnToDisplay. This is because Arrays have the first element (column so to speak in this case) at index 0 and desiredLiteralColumnNumber holds a literal value of 3. For example, if we want column 1 data, literalDataColumnToDisplay would contain value of 1, to get the actual first element from the array we need to subtract 1 to get an index value of 0.

This should get you started.

EDIT: Based on comment:

As you already know the StockFileReader#readFileData() method returns a List Interface of String (List<String>) so, you will need to need to then iterate through the List to display the desired Column, for example:

// Assuming i is the column number.
// Don't have a clue why this method returns a double because you supply no information whatsoever.
public double runAggregator(int i) throws IOException {    
    StockFileReader stReader = new StockFileReader(file);

    List<String> dataLines = stReader.readFileData();

    for (String line : dataLines) {
        String[] columnData = line.split("\\s*,\\s*);
        System.out.println(columnData[i - 1]);
    }

    // whatever else you want to do in this method.
}

Perhaps what you want to do is return all of a specific column in double data type as an Array rather than a single double. If this is the case then perhaps this is a way to go:

public double[] runAggregator(int i) throws IOException {    
    StockFileReader stReader = new StockFileReader(file);

    List<String> dataLines = stReader.readFileData();
    double[] columnArray = new double[dataLines.size()];

    for (int i = 0; i < dataLines.size(); i  ) {
        String[] columnData = dataLines.get(i).split("\\s*,\\s*);
        columnArray[i] = Double.parseDouble(columnData[i - 1]);
    }

    return columnArray;
}

To use and display the returned result:

int columnNumber = 3;
try {
    double[] columnValues = runAggregator(int columnNmber);
} catch(IOException ex) {
    System.err.println(ex.getMessage());
}

// Display column data:
System.out.println("Column "   columnNumber   " Data:
for (double d : columnValues) {
    System.out.println(d);
}
  • Related