我想提取CSV文件的特定单列值。因此我使用了CSV文件的数据集,其实例:45211和属性数:17。 我试过这个代码..但它给我这样的错误.. "主" java.lang.ArrayIndexOutOfBoundsException:3
请帮助我... import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
String filename ="bank-full.csv";
File file= new File(filename);
try {
Scanner inputStream = new Scanner(file);
inputStream.next();
while(inputStream.hasNext())
{
String data= inputStream.next();
String[] values = data.split(",");
// double balance= Double.parseDouble(values[2]);
System.out.println(values[3]);
}
inputStream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 0 :(得分:1)
我假设你想一次读一行:
Scanner inputStream = new Scanner(file);
while(inputStream.hasNextLine())
{
String data = inputStream.nextLine();
String[] values = data.split(",");
System.out.println(values[3]); // now, this line is safe only if you are
// sure that each row would have at least 3 commas
}
答案 1 :(得分:0)
(1)你确定文件中的分隔符是逗号吗?
(2)文件中可以有空的“列”吗?如果有,请使用以下内容进行拆分:
String[] values = data.split(",", -1);