我有一个带有一些变量的'DataReader'类,我想在其中输入文件中的值。
对于预算(txt文件的第一行),项目数量(第二行);部门数量(第三行)没有问题。
问题在于我正在尝试填写双打数组。
我发现的其他问题是设置数组的长度。我把这个例子放在4000中,因为我的文件是4003行(前3个是预算,#project和#systems)。但实际上数组应该与文件的长度一样长(不一定是4000)
public DataReader() {
this.budget = 0;
this.numberOfDepartments = 0;
this.numberOfProjects = 0;
this.array = new double[4000][this.numberOfProjects];
}
public void readDataFromFile() throws FileNotFoundException {
String string = null;
FileReader file = new FileReader("C:/Users/Andy/Desktop/myfile.txt"); //open a communication buffer with the txt file
Scanner reader = new Scanner(file); //Initialize a scanner to read the txt file
/* READ THE BUDGET */
string = reader.nextLine(); //read the first line
this.budget = Integer.parseInt(removeOneCommaFromString(string)); //remove the comma and transform it in an integer
System.out.println(this.budget); //test
/* READ THE NUMBER OF DEPARTMENTS */
string = reader.nextLine();
this.numberOfDepartments = Integer.parseInt(removeOneCommaFromString(string));
System.out.println(this.numberOfDepartments);
/* READ THE NUMBER OF PROJECTS */
string = reader.nextLine();
this.numberOfProjects = Integer.parseInt(removeOneCommaFromString(string));
System.out.println(this.numberOfProjects);
/* READ THE MATRIX */
for(int i = 0; i < 4000; i++) {
string = reader.nextLine();
string = removeCommasFromString(string);
char[] temp = string.toCharArray();
char[] temp2 = new char[temp.length];
for(int z = 0; z < this.numberOfProjects; z++) {
for(int j = 0; j < temp.length; j ++) {
if(temp[j] >= 0 || temp[j] <= 9 || temp[j] == '.') {
temp2[j] = temp[j];
}
else {
String newString = new String(temp2);
Double number = new Double(newString);
array[i][z] = number;
System.out.println(array[i][z]);
}
}
}
}
}
private String removeCommasFromString(String string) {
char[] stringWithoutCommas = new char[string.length()];
char[] stringWithCommas = string.toCharArray();
for(int i = 0; i < stringWithCommas.length; i++) {
if(stringWithCommas[i] != ',') {
stringWithoutCommas[i] = stringWithCommas[i];
}
else {
stringWithoutCommas[i] = ' ';
}
}
String correctString = new String(stringWithoutCommas);
return correctString;
}
private String removeOneCommaFromString(String string) {
// TODO Auto-generated method stub
char[] array1 = string.toCharArray();
char[] array2 = new char[array1.length -1];
for(int i = 0; i < array1.length-1; i++) {
array2[i] = array1[i];
}
String stringWithoutComma = new String(array2);
return stringWithoutComma;
}
public static void main(String[] args) throws FileNotFoundException {
DataReader ld = new DataReader();
ld.readDataFromFile();
}
}
这是我正在阅读的txt文件的示例,其中第一行是预算,第二行是部门数量,第三行是项目数量:
3420,
2,
10,
103.8421,1250.434,54.56157,36.92909,962.7694,173.022,106.7644,404.1759,11.66381,2966.675,
0.1270486,0.1229978,0.06825768,0.2493465,0.1555537,0.05003887,0.2638902,0.1703413,0.2503172,0.1844837,
104.5753,446.7093,825.9758,1220.173,726.259,508.811,362.4495,758.9695,258.2516,1955.544,
0.05048114,0.07712628,0.1827393,0.06109419,0.1818522,0.1821966,0.1377791,0.07180541,0.07752416,0.06139474,
444.7019,461.5938,976.8734,273.714,184.7028,229.7422,130.7461,84.34789,49.8125,417.2332,
0.1141632,0.06448551,0.08264564,0.08626967,0.2759337,0.2762382,0.1682017,0.2879532,0.1430924,0.2055033,
53.11755,479.1441,49.23315,188.405,1127.118,117.5416,105.4314,78.87017,50.0128,719.333,