我遇到问题,我正在尝试访问 JAVA 中 CSV 存档中的列。我编写了一个函数,该函数返回 csv 存档中的所有数据。但我想根据条件返回 1 列。条件是,列区域。
如果区域编号为 1,则结果为 SQL 示例。
选择区域,sum(nviv) AS 'HOUSES' FROM census WHERE region = 1
该子句返回区域 1 的 1 个值,即所有列 nviv 的总和。我尝试做同样的事情,但是在 JAVA 和 OpenCSV 中。
这是我的代码,我是使用 CSV 存档进行 Java 编程的新手。
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();
最佳答案
添加代码注释以进行解释
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;
}
关于java - 尝试访问 Java CSV 列时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62252150/