试图访问Java CSV列的问题

问题描述 投票:0回答:1

我有问题,我正在尝试从JAVA中的CSV归档访问列。我编写了一个函数,该函数从csv存档返回所有数据。但是我想根据条件返回1列。条件是列区域。

如果区域号为1,则结果例如是在SQL中。

选择区域,根据人口普查区域中“房屋”的总和(nviv)= 1

该区域返回1值,即所有列nviv的总和。我尝试这样做,但是在JAVA和OpenCSV中。

这是我的代码,我是Java编程的CSV档案库的新手。

package hogares;

import java.util.*;
import com.opencsv.*;
import java.io.*;

public class Hogares {

    public static final String SEPARADOR = ";";

    public static void main(String[] args){

        int opc;
        Scanner sc = new Scanner(System.in);

        System.out.println("MENU HOMES");
        System.out.println("");

        System.out.println("1. Count Regions by region number");
        System.out.println("2. Average of homes of blocks by region number");
        System.out.println("3. Blocks number by region number");
        System.out.println("4. Exit");
        System.out.println("");

        System.out.println("Select an option: ");
        opc = sc.nextInt();

        switch(opc){
            case 1:
                CountHome();
                break;
            case 2:
                AverageHome();
                break;
            case 3:
                BlocksNumber();
                break;
            case 4:
                System.out.println("EXITING...");
                System.out.println("");
                break;
            default:
                System.out.println("Invalid option...");
                break;
        }
    }

    private static void CountHome(){

        int region;
        Scanner lee = new Scanner(System.in);

        BufferedReader csvr = null;
        try {


            csvr = new BufferedReader(new FileReader("C:\\Users\\ADMIN\\Desktop\\census2010.csv"));

            String[] headers = new String[]{"REGION","PROVINCE","COMMUNE","DC","AREA","ZC_LOC","ID_ZONE_LOC","NVIV","NHOME","TIPE_HOME","TIPE_OPERATIVE"};

            System.out.println("Tell me a number: ");
            region = lee.nextInt();


            String line = csvr.readLine();
            while (null!=line) {
                headers = line.split(SEPARADOR);
                System.out.println(Arrays.toString(headers));


                System.out.println(Arrays.toString(headers));

                line = csvr.readLine();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally{

            if (csvr != null) {
                try {
                    csvr.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void AverageHome() {

    }

    private static void BlocksNumber() {

    }
}

这是输出。记住,我试图根据此行的数字返回1个结果:

System.out.println("Tell me a number: ");
region = lee.nextInt();

enter image description here

java opencsv
1个回答
0
投票

添加了代码注释以进行解释

    private static void CountHome() {

        int requestedRegion;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Tell me a number: ");
        requestedRegion = scanner.nextInt();

        try {
            FileReader filereader = new FileReader("C:\\\\Users\\\\ADMIN\\\\Desktop\\\\census2010.csv");

            // Use OpenCsv to read CSV file
            CSVReader csvReader = new CSVReader(filereader);

            System.out.println("REGION\tHOUSES");

            //Assign it to skip header row from records
            String[] nextLine = csvReader.readNext();

            //Take variable to for sum of nviv
            int houses = 0;

            // Read CSV record one by one
            while ((nextLine = csvReader.readNext()) != null) {

                // Get first column value which is region
                String region = nextLine[0];

                // Get nviv column value
                String nviv = nextLine[7];

                // Check region and nviv is not null and empty
                if (region != null && !region.isEmpty() && nviv != null && !region.isEmpty()) {

                    // Parse region string to integer
                    int currentRegion = Integer.parseInt(region);

                    // Check row region is equals to region entered by user
                    if (currentRegion == requestedRegion) {

                        // Parse nviv sting to integer
                        int currentNviv = Integer.parseInt(nviv);

                        // Sum nviv And get total houses
                        houses = houses + currentNviv;

                        // System.out.println(region + "\t" + nviv);
                    }
                } else {
                    System.out.println("Invalid data to proceed");
                }
            }
            System.out.println(requestedRegion + "\t" + houses);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

示例CSV数据:

REGION,PROVINCE,COMMUNE,DC,AREA,ZC_LOC,ID_ZONE_LOC,NVIV,NHOME,TIPE_HOME,TIPE_OPERATIVE
1,2,33,4,5,66,7,8,9,10,11
2,2,33,4,5,66,7,8,9,10,11
1,2,33,4,5,66,7,8,9,10,11
2,2,33,4,5,66,7,8,9,10,11
1,2,33,4,5,66,7,8,9,10,11
1,2,33,4,5,66,7,8,9,10,11
7,2,33,4,5,66,7,8,9,10,11
7,2,33,4,5,66,7,8,9,10,11
7,2,33,4,5,66,7,8,9,10,11
7,2,33,4,5,66,7,8,9,10,11
7,2,33,4,5,66,7,8,9,10,11
8,2,33,4,5,66,7,8,9,10,11
8,2,33,4,5,66,7,8,9,10,11
8,2,33,4,5,66,7,8,9,10,11
8,2,33,4,5,66,7,8,9,10,11
8,2,33,4,5,66,7,8,9,10,11

输出:

MENU HOMES

1. Count Regions by region number
2. Average of homes of blocks by region number
3. Blocks number by region number
4. Exit

Select an option: 
1
Tell me a number: 
7
REGION  HOUSES
7   40

如果CSV的行为空,会导致错误,

检查是否存在任何空行,如果找到则跳过

           // Read CSV record one by one
            while ((nextLine = csvReader.readNext()) != null) {

                // Check row empty, if found skip iteration
                if (nextLine.length != 0 && nextLine[0].trim().isEmpty()) {
                    continue;
                }
© www.soinside.com 2019 - 2024. All rights reserved.