I made a 2d array chart that shows and calculates the average noise levels of each car model as my school project. Now I need to have a separate file to store my array values and put them in the chart. I've tried to add the values in using a while loop but it gives me this error. How can I add values from my text file to the array? I am still new to 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)"
This is my whole code:
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.");
}
}
Also, ignore the values I already have in my array.