gpt4 book ai didi

java - 如何将文本文件数组中的数字添加到二维 int 数组中?

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:34 26 4
gpt4 key购买 nike

作为我的学校项目,我制作了一个二维数组图表,显示并计算了每种车型的平均噪音水平。现在我需要一个单独的文件来存储我的数组值并将它们放入图表中。我尝试使用 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.");

}
}

此外,忽略数组中已有的值。

最佳答案

我注意到的一个问题是:

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时出现异常,那么您将无法使用它,因为它实际上尚未创建。

此外,我强烈建议在出现异常时打印出完整的异常详细信息(e.toString() 将打印出异常消息和堆栈跟踪),而不是在出现异常时打印出“您遇到错误”。显然,您不想在实际生产系统中向用户显示它,但出于学习目的,如果您显示(或至少记录)完整的详细信息,则调试会变得容易得多。 (在生产系统中,我们通常会创建某种类型的日志,其中包含此类异常的完整详细信息,以便我们可以更轻松地进行调试)。

关于java - 如何将文本文件数组中的数字添加到二维 int 数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44292337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com