我的程序遇到问题,因为在主方法中我正在读取文本文件,然后我使用split方法解析数组。此外,我将它传递给另一个将操纵文件的方法,但终端中没有显示任何内容。 Txt file to read 主要方法:
Scanner scan = new Scanner (System.in);
BufferedReader br = null;
String line;
System.out.println ("Please enter the filename to read:");
try {
br = new BufferedReader (new FileReader (scan.next()));
} catch (FileNotFoundException fnfex) {
System.out.println (fnfex.getMessage());
System.exit(0);
}
try {
String[][] str = new String[7][3];
int counter =0;
while ((line = br.readLine()) !=null){
str[counter] = line.split(" ");
counter++;
}
int [][] calories = new int [7][3];
int count =0;
for (int i =0; i< calories.length; i++){
for (int j=0; j< calories.length; j++){
count++;
calories[i][j] = Integer.parseInt(str[i][j]);
//System.out.println (str[i][j] );
}
}
totCalories(calories);
-------------------------------------------------------------------------------
Method were I modify the file:
public static void totCalories (int [][] a){
int sum =0;
for (int i=0; i<a.length; i++){
for (int j =0; j<a[i].length; j++){
sum += a[i][j];
}
System.out.println ("This is the total number of calories consumed from Monday to Sunday: " + sum);
}
}