在java中将CSV复制到数组时出现问题
所以这是我第一次尝试做这样的事情,我从网上的各种渠道收集了关于如何正确做这件事的信息。到目前为止,我已经编写了一个代码,我相信它将能够完成这项工作。但是,每当我单击“运行”时,程序将终止并打印此错误:在java中将CSV复制到数组时出现问题,java,csv,Java,Csv,所以这是我第一次尝试做这样的事情,我从网上的各种渠道收集了关于如何正确做这件事的信息。到目前为止,我已经编写了一个代码,我相信它将能够完成这项工作。但是,每当我单击“运行”时,程序将终止并打印此错误: Exception in thread "main" java.lang.NumberFormatException: For input string: "Version,1.4.0" at sun.misc.FloatingDecimal.readJavaFormatString(Fl
Exception in thread "main" java.lang.NumberFormatException: For input string: "Version,1.4.0"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Double.parseDouble(Double.java:540)
at IO_CSV.setUpMyCSVArray(IO_CSV.java:47)
at IO_CSV.main(IO_CSV.java:82)
这是我的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.NumberFormat;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class IO_CSV {
static String xStrPath;
static double[][] myArray;
NumberFormat nf = NumberFormat.getInstance();
static void setUpMyCSVArray()
{
myArray = new double[3000][3000];
Scanner scanIn = null;
int RowC= 0;
int Row = 0;
int ColC = 0;
int Col = 0;
String InputLine = " ";
String xfileLocation;
xfileLocation = "/Users/victorgarcia/Documents/reza_data1.csv";
try
{
//setup scanner
scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));
//while ((InputLine = scanIn.nextLine()) != null)
while(scanIn.hasNextLine())
{
//read line in from file
InputLine = scanIn.nextLine();
//split the InputLine into an array at the commas
String[] InArray = InputLine.split(",,,");
//copy the content of the inArray to the myArray
for(int x = 0; x< InArray.length; x++)
{
myArray[RowC][x] = Double.parseDouble(InArray[x]);
}
//increment the row in the array
RowC++;
}
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
static void printMyArray()
{
for (int RowC=0; RowC<3000; RowC++)
{
for (int ColC=0; ColC<3000; ColC++)
{
System.out.print(myArray[RowC][ColC]+ " ");
}
System.out.println();
}
return;
}
public static void main(String[] args)
{
setUpMyCSVArray();
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.text.NumberFormat;
导入java.util.Scanner;
导入java.io.FileNotFoundException;
公共类IO_CSV{
静态字符串xStrPath;
静态双[][]myArray;
NumberFormat nf=NumberFormat.getInstance();
静态无效设置mycsvarray()
{
myArray=新的双精度[3000][3000];
扫描器scanIn=null;
int-RowC=0;
int行=0;
int ColC=0;
int Col=0;
字符串InputLine=“”;
字符串位置;
xfileLocation=“/Users/victorgarcia/Documents/reza_data1.csv”;
尝试
{
//安装扫描仪
scanIn=newscanner(newbufferedreader(newfilereader(xfileLocation));
//而((InputLine=scanIn.nextLine())!=null)
while(scanIn.hasNextLine())
{
//从文件中读取行
InputLine=scanIn.nextLine();
//将输入线拆分为逗号处的数组
字符串[]InArray=InputLine.split(“,”);
//将inArray的内容复制到myArray
对于(int x=0;x 对于(int RowC=0;RowC,在这段代码中,您假设CSV中的所有值都可以解析为double。对于文件中明显出现的字符串“Version,1.4.0”,情况并非如此。要发现此问题并记录无法将值解析为double的所有其他情况,您可以如下更改代码:
try
{
myArray[RowC][x] = Double.parseDouble(InArray[x]);
}
catch (NumberFormatException nfe)
{
System.out.println("Unable to parse at " + rowC + ", " + x + ": " + InArray[x]);
}
虽然设计一个简单的自制CSV解析器相对容易,但实际上这是一个比表面上看起来更复杂的问题。最终,你可能会花费相当多的时间来解决像现在这样的问题,所以通常不值得实现你自己的问题。有很多开放的资源e实现可用(例如)。您能给我们一个示例输入文件吗?您在解析它的过程中弄错了,所以最好有一个示例来指导您。您还可以注意:您的printMyArray()方法可以改进。您只需执行以下操作,而不是硬编码值:静态void printArray(double[][]toPrint){for(intx=0;x