java 我如何将所需的数据从csv直接放入变量中?

z9smfwbn  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(103)

我有.csv数据文件要解析,并为此使用java语言。因此,我理解了如何使用数据并将其放入变量中,例如,使用二维数组:

//working with 2-dimensial Arrays
int[][] values = { {1, 2, 3, 4, 5}, 
                   {6, 7, 8, 9, 10}, 
                   {11, 12, 13, 14, 15}, 
                   {16, 17, 18, 19, 20}, 
                   {21, 22, 23, 24, 25} };
                  
//output of the 1st column, 1st line array value 
System.out.println(values[0][0]);

    ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles", "dough"));

    String firstName = list.get(0);         
    String secondName = list.get(1); 
    
    System.out.println(firstName);

但我需要先读取.csv文件,然后创建多维数组。在此之后,我需要拾取一些数据并将其放入变量中。我读csv没有任何具体的库,只有这样的标准:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

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

        String fileName= "datatry.csv";
        File file= new File(fileName);

      
        List<List<String>> lines = new ArrayList<>();
   
        Scanner inputStream;
//this scanner of csv doesn't work without try-catch pair
        try{
            //read the csv file 
            inputStream = new Scanner(file);

            while(inputStream.hasNext()){
                String line= inputStream.next();
                String[] values = line.split(";");
                // this adds the currently parsed line to the 2-dimensional string array
                lines.add(Arrays.asList(values));
            }
//System.out.println(value);

            inputStream.close();
        }
        
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      //  String newst = lines.get(1);
//System.out.println(newst);
        // the following code lets you iterate through the 2-dimensional array
        int lineNo = 1;
        for(List<String> line: lines) {
            int columnNo = 1;
            for (String value: line) {
                System.out.println("Line " + lineNo + " Column " + columnNo + ": " + value);
                columnNo++;
            }
            lineNo++;
        }
        }}

我想我需要使用Object。
如何将.csv文件的第三列和第五行的值放入整型变量中?

wwtsj6pe

wwtsj6pe1#

伪代码:
1.设置targetRoNumber和targetColNumber
1.创建2个for循环来跟踪索引。第一个循环用于行,第二个循环用于列。
1.创建if条件以匹配目标行号和列号

String csvFilePath = "D:\\Project\\sample.csv";

        try {
            Path path = Paths.get(csvFilePath);
            List<String> lines = Files.readAllLines(path);
            int targetRowNumber=5;
            int targetColNumber=3;
            //Integer extractedNumber=null;
            for(int row=0;row<lines.size();row++) {
                String line = lines.get(row);
                String[] fields = line.split(",");
                
                for(int col=0;col<fields.length;col++) {
                    if(row==targetRowNumber && col==targetColNumber) {
                        int extractedNumber = Integer.parseInt(fields[col]);
                        System.out.println(extractedNumber);
                    }
                }   
            }
        } catch (IOException e) {
            e.printStackTrace();
        }catch (NumberFormatException e) {
            e.printStackTrace();
        }

或者如果你不想循环,直接进入索引

String csvFilePath = "D:\\Project\\sample.csv";
        int targetRowNumber = 5;
        int targetColNumber = 3;

        try {
            List<String> lines = Files.readAllLines(Paths.get(csvFilePath));

            if (targetRowNumber < lines.size()) {
                String[] fields = lines.get(targetRowNumber).split(",");

                if (targetColNumber < fields.length) {
                    int extractedNumber = Integer.parseInt(fields[targetColNumber]);
                    System.out.println(extractedNumber);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

相关问题