如何将文本文件数组中的数字添加到2d int数组?

时间:2017-05-31 18:35:08

标签: java multidimensional-array

我制作了一个二维阵列图表,显示和计算每个车型的平均噪音水平作为我的学校项目。现在我需要一个单独的文件来存储我的数组值并将它们放在图表中。我尝试使用while循环添加值,但它给了我这个错误。如何将文本文件中的值添加到数组中?我还是java的新手。

public static void writetoarray(){
 try{
    k = new Scanner(new File("test.txt"));

 }
catch(Exception e){
System.out.println("You got an error");
}
 int i = 0;
 while(k.hasNextInt()){
    firstarray[0][i] = k.nextInt();
    firstarray[1][i] = k.nextInt();
    firstarray[2][i] = k.nextInt();
    firstarray[3][i] = k.nextInt();
    firstarray[4][i] = k.nextInt();
    firstarray[5][i] = k.nextInt();
    firstarray[6][i] = k.nextInt();
    i++;
    }
  }


"Exception in thread "main" java.lang.NullPointerException
 at practice.writetoarray(practice.java:29)
 at practice.main(practice.java:11)"

这是我的全部代码:

import java.io.File;
import java.util.*;
public class practice {
public static Scanner k;
public int foo = Integer.parseInt(k.next());    
public static int n = 1;
public static int firstarray[][] ={{81,90,92,103,111,121,132,0},
{78,85,90,99,104,111,118,0},{80,86,91,95,100,108,119,0},
{87,90,95,101,111,121,133,0},{66,70,76,86,96,115,125,0},
{81,83,85,93,102,113,122,0},{76,78,80,85,94,104,114,0},{0,0,0,0,0,0,0,0}};
public static int y = 0;    
public static void main(String[] args) {
    String tableheadings[][]={{"\t\t\t\t    MPH"+"                              
|"},{"Model|","20","30","40","50","60","70","80","Avg     |",},
{"_____________________________________
___________________________________|"}};


writetoarray();
displayheader(tableheadings);
countArraytotal(firstarray);
arrayavg(firstarray);
arrayavgc(firstarray);
printrows(firstarray);
countRowandCol(firstarray);
}


 public static void writetoarray(){
 try{
    k = new Scanner(new File("test.txt"));

 }
catch(Exception e){
System.out.println("You got an error");
}
 int i = 0;
 while(k.hasNextInt()){
    firstarray[0][i] = k.nextInt();
    firstarray[1][i] = k.nextInt();
    firstarray[2][i] = k.nextInt();
    firstarray[3][i] = k.nextInt();
    firstarray[4][i] = k.nextInt();
    firstarray[5][i] = k.nextInt();
    firstarray[6][i] = k.nextInt();
    i++;
   }

 }



 public static void displayheader(String a[][]){
    for (int row=0 ;row<a.length;row++){
        for(int column=0; column<a[row].length;column++){
        System.out.print(a[row][column]+"\t");
        }
    System.out.println();
}
}

public static void printrows(int a[][]){

for (int row=0 ;row<a.length;row++){
    if(row<a.length-1){
    System.out.print(n+++"    |\t");
    }
    else{System.out.print("Avg  |\t");}
    for(int column=0; column<a[row].length;column++){

        System.out.print(a[row][column]+"\t");

    }

    System.out.print("|");
    System.out.println();
 }
}

public static void arrayavg(int[][] array) {
int s=   0;
for (int i = 0; i < array.length; i++){ s=0;
for (int j = 0; j < array[i].length; j++){             
s += array[i][j];
}
firstarray[i][7] = Math.round(s/(array.length-1));  
 }
}

public static void arrayavgc(int[][] array) {
int s=   0;

for (int i = 0; i < array.length; i++){ s=0;
for (int j = 0; j < array[i].length; j++){             
s += array[j][i];
}
firstarray[7][i] = Math.round(s/(array[i].length-1));
}
}

public static void countArraytotal(int[][] table){
int total=0;

for (int row=0;row<table.length;row++)
for (int col=0;col<table[0].length;col++)
  total += table[row][col];
firstarray[7][7]=Math.round(total/49);
}

public static void countRowandCol(int[][] array){
int rowsize = array.length;
int columnSize = array.length;
System.out.println("There are " + rowsize + " rows and " + columnSize+ " 
columns in this table.");

}
}

另外,忽略我在数组中已有的值。

1 个答案:

答案 0 :(得分:0)

我注意到的一个问题是:

try{
    k = new Scanner(new File("test.txt"));
 }
catch(Exception e){
    // You really should print out the full exception details here
    // It'll make debugging much easier
    System.out.println("You got an error");
}

// If you keep going after an exception here, you'll get a NullPointerException
// because k isn't initialized validly
int i = 0;
while(k.hasNextInt()){
    firstarray[0][i] = k.nextInt();
    firstarray[1][i] = k.nextInt();
    firstarray[2][i] = k.nextInt();
    firstarray[3][i] = k.nextInt();
    firstarray[4][i] = k.nextInt();
    firstarray[5][i] = k.nextInt();
    firstarray[6][i] = k.nextInt();
    i++;
}

如果您尝试初始化k时出现异常,则您无法使用它,因为它实际上并未创建。

此外,而不是打印&#34;你有一个错误&#34;在出现异常的情况下,我强烈建议打印出完整的异常详细信息(e.toString()将打印出异常消息和堆栈跟踪)。显然,您不希望在实际生产系统中向用户显示该内容,但出于学习目的,如果您显示(或至少记录)完整详细信息,则可以更轻松地进行调试。 (在生产系统中,我们通常使用这样的异常的完整详细信息创建某种类型的日志,以便我们可以更轻松地进行调试)。