因此,這是我第一次嘗試這樣做,並且我一直在線收集各種來源的信息,以瞭解如何正確執行此操作。到目前爲止,我已經寫了一個我相信能夠完成工作的代碼。但是,每當我點擊運行,程序將終止並打印此錯誤:將java複製到java中的數組時遇到問題
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();
}
}
希望有人對我一個答案,我在做什麼錯
你能給我們一個樣本輸入文件嗎?你錯誤地解析它,所以有個例子可以指導你 – Dici 2014-10-31 20:02:55
另外要注意:你的printMyArray()方法可以改進。而不是硬編碼的值你可以簡單地做:靜態無效printArray(double [] [] toPrint){for(int x = 0; x
user2494817
2014-10-31 20:07:02