关于java从文本读取一个二维数组,然后简单计算,再写出的问题
文本:788778785478现在在前面加!!!这句,会报空指针异常,不知道为什么publicclassmysort{publicstaticvoidmain(Strin...
文本:
78 87
78 78
54 78
现在在前面加!!!这句,会报空指针异常,不知道为什么
public class mysort {
public static void main(String[] args) throws FileNotFoundException,IOException{
int[][] aryBefore=getAry("myStudentScore.txt");
plusScore(aryBefore);
// ouputScore(aryBefore,"myStudentScore.txt");
}
private static void ouputScore(int[][] aryBefore, String string) {
}
private static void plusScore(int[][] aryBefore){
// System.out.println(aryBefore[0][0]);
int[] sum=new int[aryBefore.length];
for (int i = 0; i < aryBefore.length; i++) {
for (int j = 0; j < aryBefore[i].length; j++) {
sum[j]+=aryBefore[i][j];
}
}
System.out.println(Arrays.toString(sum));
}
private static int[][] getAry(String string) throws FileNotFoundException,IOException {
BufferedReader bufR=new BufferedReader(new FileReader(string));
String lineSrc;
int row=0;
int[][] getAry=new int[0][0];
while ((lineSrc=bufR.readLine())!=null) {
getAry=Arrays.copyOf(getAry, getAry.length+1);
String[] everScore=lineSrc.split(" ");
for (int i = 0; i < everScore.length; i++) {
!!!! getAry[row]=Arrays.copyOf(getAry[row], getAry[row].length+1);
getAry[row][i]=Integer.parseInt(everScore[i]);
// System.out.println(getAry[row][i]+"+"+row);
row++;
}
lineSrc=bufR.readLine();
}
return getAry;
}
} 展开
78 87
78 78
54 78
现在在前面加!!!这句,会报空指针异常,不知道为什么
public class mysort {
public static void main(String[] args) throws FileNotFoundException,IOException{
int[][] aryBefore=getAry("myStudentScore.txt");
plusScore(aryBefore);
// ouputScore(aryBefore,"myStudentScore.txt");
}
private static void ouputScore(int[][] aryBefore, String string) {
}
private static void plusScore(int[][] aryBefore){
// System.out.println(aryBefore[0][0]);
int[] sum=new int[aryBefore.length];
for (int i = 0; i < aryBefore.length; i++) {
for (int j = 0; j < aryBefore[i].length; j++) {
sum[j]+=aryBefore[i][j];
}
}
System.out.println(Arrays.toString(sum));
}
private static int[][] getAry(String string) throws FileNotFoundException,IOException {
BufferedReader bufR=new BufferedReader(new FileReader(string));
String lineSrc;
int row=0;
int[][] getAry=new int[0][0];
while ((lineSrc=bufR.readLine())!=null) {
getAry=Arrays.copyOf(getAry, getAry.length+1);
String[] everScore=lineSrc.split(" ");
for (int i = 0; i < everScore.length; i++) {
!!!! getAry[row]=Arrays.copyOf(getAry[row], getAry[row].length+1);
getAry[row][i]=Integer.parseInt(everScore[i]);
// System.out.println(getAry[row][i]+"+"+row);
row++;
}
lineSrc=bufR.readLine();
}
return getAry;
}
} 展开
展开全部
有两个问题:
1、叹号这一行里的 getAry[row] 没有初始化,所以NULL错误
2、row++应该在for循环外
我给你改了一下:
private static int[][] getAry(String string) throws FileNotFoundException, IOException {
BufferedReader bufR = new BufferedReader(new FileReader(string));
String lineSrc;
int row = 0;
int[][] getAry = new int[0][0];
while ((lineSrc = bufR.readLine()) != null) {
getAry = Arrays.copyOf(getAry, getAry.length + 1);
String[] everScore = lineSrc.split(" ");
getAry[row] = new int[0]; //这里先初始化一下getAry[row],否则会报NULL错
for (int i = 0; i < everScore.length; i++) {
getAry[row] = Arrays.copyOf(getAry[row], getAry[row].length + 1);
getAry[row][i] = Integer.parseInt(everScore[i]);
// System.out.println(getAry[row][i]+"+"+row);
}
row++; //这个row++是行递增,应该在for之外
lineSrc = bufR.readLine();
}
return getAry;
}
1、叹号这一行里的 getAry[row] 没有初始化,所以NULL错误
2、row++应该在for循环外
我给你改了一下:
private static int[][] getAry(String string) throws FileNotFoundException, IOException {
BufferedReader bufR = new BufferedReader(new FileReader(string));
String lineSrc;
int row = 0;
int[][] getAry = new int[0][0];
while ((lineSrc = bufR.readLine()) != null) {
getAry = Arrays.copyOf(getAry, getAry.length + 1);
String[] everScore = lineSrc.split(" ");
getAry[row] = new int[0]; //这里先初始化一下getAry[row],否则会报NULL错
for (int i = 0; i < everScore.length; i++) {
getAry[row] = Arrays.copyOf(getAry[row], getAry[row].length + 1);
getAry[row][i] = Integer.parseInt(everScore[i]);
// System.out.println(getAry[row][i]+"+"+row);
}
row++; //这个row++是行递增,应该在for之外
lineSrc = bufR.readLine();
}
return getAry;
}
展开全部
你 这句代码 getAry=Arrays.copyOf(getAry, getAry.length+1);
取得getAry 是null 所以getAry[row] 就会报空指针异常了
取得getAry 是null 所以getAry[row] 就会报空指针异常了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据下标是从0开始的,你用getAry[row].length+1会越界,把+1去掉吧
追问
但是,这不是吧原来的数组长度+1吗,
追答
数组的长度是不可变的,你要长度+1,要重新定义一个数组。
这样:int getAry2[row+1]=Arrays.copyOf(getAry[row], getAry[row].length+1);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询