将文本文件解析为锯齿状数组
Parsing text file to jagged array
我有以下文件
3
2,3,4,5
6,7,8
9,10
并且我正在尝试将其转换为将其作为锯齿状双精度数组传递。我的意思是,我试图将其存储为
double[][] myArray = {{2,3,4},{6,7},{9}}
double[] secondArray = {5,8,10}
我已经能够从文件中读取值,但我被困在两件事上。
- 如何将值转换为双精度数组?
- 如何将最后的元素存储到新数组中?
我遇到错误是因为我的数组包含逗号分隔值,但如何才能将单个值转换为双精度值?我对 Java 还是个新手,所以我不知道所有的内置方法。
这是我目前所拥有的
public double[] fileParser(String filename) {
File textFile = new File(filename);
String firstLine = null;
String secondLine = null;
String[] secondLineTokens = null;
FileInputStream fstream = null;
try {
fstream = new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
try {
firstLine = br.readLine(); // reads the first line
List<String> myList = new ArrayList<String>();
while((secondLine = br.readLine()) != null){
myList.add(secondLine);
//secondLineTokens = secondLine.split(",");
}
String[] linesArray = myList.toArray(new String[myList.size()]);
for(int i = 0; i<linesArray.length; i++){
System.out.println("tokens are: " + linesArray[i]);
}
double[] arrDouble = new double[linesArray.length];
for(int i=0; i<linesArray.length; i++)
{
arrDouble[i] = Double.parseDouble(linesArray[i]); #error here
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
看起来第一行给出了文件其余部分的行数。您可以利用它预先制作数组,如下所示:
int n = Integer.parseInt(br.readLine());
double a[][] = new double[n][];
double b[] = new double[n];
for (int i = 0 ; i != n ; i++) {
String[] tok = br.readLine().split(",");
a[i] = new double[tok.length-1];
for (int j = 0 ; j != a[i].length ; j++) {
a[i][j] = Double.parseDouble(tok[j]);
}
b[i] = Double.parseDouble(tok[tok.length-1]);
}
同样,您可以使用String.split
方法找出要添加到交错数组中的条目数。这样代码会变得更短,因为您可以预先分配所有数组。
我有以下文件
3
2,3,4,5
6,7,8
9,10
并且我正在尝试将其转换为将其作为锯齿状双精度数组传递。我的意思是,我试图将其存储为
double[][] myArray = {{2,3,4},{6,7},{9}}
double[] secondArray = {5,8,10}
我已经能够从文件中读取值,但我被困在两件事上。
- 如何将值转换为双精度数组?
- 如何将最后的元素存储到新数组中?
我遇到错误是因为我的数组包含逗号分隔值,但如何才能将单个值转换为双精度值?我对 Java 还是个新手,所以我不知道所有的内置方法。
这是我目前所拥有的
public double[] fileParser(String filename) {
File textFile = new File(filename);
String firstLine = null;
String secondLine = null;
String[] secondLineTokens = null;
FileInputStream fstream = null;
try {
fstream = new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
try {
firstLine = br.readLine(); // reads the first line
List<String> myList = new ArrayList<String>();
while((secondLine = br.readLine()) != null){
myList.add(secondLine);
//secondLineTokens = secondLine.split(",");
}
String[] linesArray = myList.toArray(new String[myList.size()]);
for(int i = 0; i<linesArray.length; i++){
System.out.println("tokens are: " + linesArray[i]);
}
double[] arrDouble = new double[linesArray.length];
for(int i=0; i<linesArray.length; i++)
{
arrDouble[i] = Double.parseDouble(linesArray[i]); #error here
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
看起来第一行给出了文件其余部分的行数。您可以利用它预先制作数组,如下所示:
int n = Integer.parseInt(br.readLine());
double a[][] = new double[n][];
double b[] = new double[n];
for (int i = 0 ; i != n ; i++) {
String[] tok = br.readLine().split(",");
a[i] = new double[tok.length-1];
for (int j = 0 ; j != a[i].length ; j++) {
a[i][j] = Double.parseDouble(tok[j]);
}
b[i] = Double.parseDouble(tok[tok.length-1]);
}
同样,您可以使用String.split
方法找出要添加到交错数组中的条目数。这样代码会变得更短,因为您可以预先分配所有数组。