CSV数据到2d数组java

CSV数据到2d数组java,java,file,Java,File,我可能走错了方向,但我的问题是,我该如何填充fxRates的数组 CAD,EUR,GBP,USD 1.0,0.624514066,0.588714763,0.810307 1.601244959,1.0,0.942676548,1.2975 1.698615463,1.060809248,1.0,1.3764 1.234100162,0.772200772,.726532984,1.0 这是CSV文件中的信息,我正在考虑使用scanner类来读取它。差不多 private double[][]

我可能走错了方向,但我的问题是,我该如何填充fxRates的数组

CAD,EUR,GBP,USD
1.0,0.624514066,0.588714763,0.810307
1.601244959,1.0,0.942676548,1.2975
1.698615463,1.060809248,1.0,1.3764
1.234100162,0.772200772,.726532984,1.0
这是CSV文件中的信息,我正在考虑使用scanner类来读取它。差不多

private double[][] fxRates;
String delimiter = ","
Scanner sc = new Scanner(file);
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        fxRates = line.split(delimiter)

你解决这个问题的方法似乎还可以。但是
line.split(“,”)
将返回一个1D字符串数组。您不能将其分配给
fxRates
。而且您还应该知道行数,以便在开始时初始化
fxRates
。否则,您应该使用动态列表结构,如
ArrayList

假设您的文件中有50行,您可以使用以下内容:

private String[][] fxRates = String[50][];
String delimiter = ",";
Scanner sc = new Scanner(file);     
int index=0;

while (sc.hasNextLine()) 
{
    String line = sc.nextLine();
    fxRates[index++] = line.split(delimiter)
}

请注意,我已将
fxRates
声明为2D字符串数组,如果需要双值,则应就地或稍后进行一些转换。

拆分操作的结果是
字符串
数组,而不是
双值
数组。因此缺少一个步骤:将字符串转换为双精度:

private double[][] fxRates = new double[maxLines][4];
String delimiter = ","
int line = 0;
Scanner sc = new Scanner(file);
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        String[] fxRatesAsString = line.split(delimiter);
        for (int i = 0; i < fxRatesAsString.length; i++) {
            fxRates[line][i] = Double.parseDouble(fxRatesAsString[i]);
        }
private double[]fxRates=new double[maxLines][4];
字符串分隔符=“,”
内线=0;
扫描仪sc=新扫描仪(文件);
while(sc.hasNextLine()){
字符串行=sc.nextLine();
String[]fxRatesAsString=line.split(分隔符);
对于(int i=0;i
另一个例子

        Double[][] fxRates = new Double[4][];
        String delimiter = ",";
        //file code goes here
        Scanner sc = new Scanner(file);
        // Read File Line By Line
        int auxI = 0;
        // Read File Line By Line
        for (int auxI =0; sc.hasNextLine(); auxI++) {
            String line = sc.nextLine();
            System.out.println(line);
            String[] fxRatesAsString = line.split(delimiter);
            Double[] fxRatesAsDouble = new Double[fxRatesAsString.length];
            for (int i = 0; i < fxRatesAsString.length; i++) {
                fxRatesAsDouble[i] = Double.parseDouble(fxRatesAsString[i]);
                }
            fxRates[auxI] = fxRatesAsDouble;
        }
        //to double check it
        for (int y =0; y<fxRates.length; y++){              
            for (int x =0; x<fxRates.length; x++){
                System.out.print(fxRates[y][x] +" ");
            }
            System.out.println("");
        }
Double[]fxRates=new Double[4][];
字符串分隔符=“,”;
//文件代码在这里
扫描仪sc=新扫描仪(文件);
//逐行读取文件
int auxI=0;
//逐行读取文件
for(int auxI=0;sc.hasNextLine();auxI++){
字符串行=sc.nextLine();
系统输出打印项次(行);
String[]fxRatesAsString=line.split(分隔符);
Double[]fxratesasdoull=新的双精度[fxratesastring.length];
对于(int i=0;i对于(It y=0;y我不建议您以这样的方式解析CSV,因为<代码>扫描器太低级别和原始的解决方案。相比之下,DOM/SAX解析器更好地解析XML而不是正则表达式解析或不考虑文档结构的任何东西。有CSV解析器,具有良好的API和SUGG。读卡器初始化期间的est配置选项。只需看一下易于使用。以下是使用它的代码示例:

q12967756包;
导入java.io.IOException;
导入java.io.StringReader;
导入java.util.ArrayList;
导入java.util.Collection;
导入静态java.lang.Double.parseDouble;
导入静态java.lang.System.out;
进口com.csvreader.csvreader;
公开期末班{
专用干管(){
}
私有静态最终字符串模拟=
CAD、欧元、英镑、美元\n+
“1.0,0.624514066,0.588714763,0.810307\n”+
“1.601244959,1.0,0.942676548,1.2975\n”+
“1.698615463,1.060809248,1.0,1.3764\n”+
“1.23410162,0.772200772,726532984,1.0\n”;
私有静态最终字符分隔符=',';
公共静态void main(字符串[]args)引发IOException{
//final FileReader contentReader=newfilereader(“yourfile.csv”);
最终StringReader contentReader=新StringReader(模拟);
最终CsvReader csv=新CsvReader(内容阅读器、分隔符);
csv.readHeaders();//跳过'CAD、EUR、GBP、USD`
最终收集温度=新的ArrayList();
而(csv.readRecord()){
临时添加(parseRawValues(csv.getValues());
}
最终双精度[]阵列2d=临时阵列(新双精度[temp.size()][]);
out.println(array2d[3][1]);
}
私有静态双[]parseRawValues(字符串[]rawValues){
最终整数长度=rawValues.length;
最终双精度[]值=新双精度[长度];
for(int i=0;i
如果手动解析CSV,那就是自找麻烦。
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;

public class CSVReader{

    private String readFile(String path, Charset encoding) throws IOException
    {
        //Read in all bytes from a file at the specified path into a byte array
        //This method will fail if there is no file to read at the specified path
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        //Convert the array of bytes into a string.
        return new String(encoded, encoding);
    }

    public String readFile(String path)
    {
        try {
            //Read the contents of the file at the specified path into one long String
            String content = readFile(path, Charset.defaultCharset());

            //Display the string.  Feel free to comment this line out.
            System.out.println("File contents:\n"+content+"\n\n");

            //Return the string to caller
            return content;

        }catch (IOException e){
            //This code will only execute if we could not open a file

            //Display the error message
            System.out.println("Cannot read file "+path);
            System.out.println("Make sure the file exists and the path is correct");

            //Exit the program
            System.exit(1);
        }`enter code here`
        return null;
    }
}