I have an assignment that requires me to create a method called getExamRange that looks at an array which includes the exam scores of several students, takes the lowest and highest scores, and subtracts the minimum exam score from the maximum exam score. I also have to create a getMostImprovedStudent which run the getExamRange method on an array of Students and returns the student with the highest exam range. I'm having trouble getting the correct results when the code is run. What is causing this problem?
Here is the code for the Student.java class:
import java.util.*;
public class Student
{
private static final int NUM_EXAMS = 4;
private String firstName;
private String lastName;
private int gradeLevel;
private double gpa;
private int[] exams;
private int numExamsTaken;
/**
* This is a constructor. A constructor is a method
* that creates an object -- it creates an instance
* of the class. What that means is it takes the input
* parameters and sets the instance variables (or fields)
* to the proper values.
*
* Check out StudentTester.java for an example of how to use
* this constructor.
*/
public Student(String fName, String lName, int grade)
{
firstName = fName;
lastName = lName;
gradeLevel = grade;
exams = new int[NUM_EXAMS];
numExamsTaken = 0;
}
public int getExamRange()
{
Arrays.sort(exams);
int examScore1 = exams[0];
int examScore2 = 0;
int lastPos = 0;
for(int i = 0; i < exams.length - 1; i++)
{
lastPos++;
}
examScore2 = exams[lastPos];
return examScore2 - examScore1;
}
public String getName()
{
return firstName + " " + lastName;
}
public void addExamScore(int score)
{
exams[numExamsTaken] = score;
numExamsTaken++;
}
// This is a setter method to set the GPA for the Student.
public void setGPA(double theGPA)
{
gpa = theGPA;
}
/**
* This is a toString for the Student class. It returns a String
* representation of the object, which includes the fields
* in that object.
*/
public String toString()
{
return firstName + " " + lastName + " is in grade: " + gradeLevel;
}
}
and here is the code for the Classroom.java class:
public class Classroom
{
Student[] students;
int numStudentsAdded;
public Classroom(int numStudents)
{
students = new Student[numStudents];
numStudentsAdded = 0;
}
public Student getMostImprovedStudent()
{
int highestScore = 0;
int score = 0;
int location = 0;
int finalLocation = 0;
for(Student s: students)
{
score = s.getExamRange();
location++;
if(score > highestScore)
{
highestScore = score;
finalLocation = location;
}
}
return students[finalLocation - 1];
}
public void addStudent(Student s)
{
students[numStudentsAdded] = s;
numStudentsAdded++;
}
public void printStudents()
{
for(int i = 0; i < numStudentsAdded; i++)
{
System.out.println(students[i]);
}
}
}
Here are the directions for the assignment which state what the methods are supposed to do:
Taking our Student and Classroom example from earlier, you should fill in the method getMostImprovedStudent, as well as the method getExamRange. The most improved student is the one with the largest exam score range.
To compute the exam score range, you must subtract the minimum exam score from the maximum exam score.
For example, if the exam scores were 90, 75, and 84, the range would be 90 - 75 = 15.
Firstly let us look at the getExamRange function
public int getExamRange(){
if(exams == null ||exams.length == 0){
return 0;
}
int min = exams[0];
int max = exams[0];
for (int i : exams
) {
if(i<min){
min=i;
}
if(i>max){
max=i;
}
}
return max - min;
}
and now on getMostImprovedStudent
public Student getMostImprovedStudent()
{
if(students == null ||students[0] == null || students.length=0){
return null;
}
int highestScore = students[0].getExamRange();
int score = 0;
Student mostImprovedStudent = students[0]
for(int i=0;i<students.length;i++)
{
if(students[i]!=null){
score = students[i].getExamRange();
if(score > highestScore)
{
highestScore = score;
mostImprovedStudent = students[i];
}
}
}
return mostImprovedStudent;
}
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 12 months ago.
I'm working on a project that involves three code files: a class for passengers - Passenger.java, a class for the airplane itself - Airplane.java, and a driver class - RunAirplane.java. 5 passengers have been added using the Passenger instantiation in main along with a count of the passengers that I plan on updating later so it is not limited to 5. The issue is that I keep getting a NullPointerException error whenever I run the main file in IntelliJ and cant find out why. The error code says it's happening on line 58 of the Airplane.java file which leads me to assume that it's because of the addPassenger method.
Passenger Class (if needed)
public class Passenger {
private String name;
private int birthYear;
private double weight;
private char gender;
private int numCarryOn;
//Default constructor
public Passenger(){
name = "";
birthYear = 1900;
weight = 0.0;
gender = 'u';
numCarryOn = 0;
}
//Alt default constructor
public Passenger(String newName, int newBirthYear, double newWeight, char newGend, int newNumCarryOn){
this.setName(newName);
this.setBirthYear(newBirthYear);
this.setWeight(newWeight);
this.setGender(newGend);
this.setNumCarryOn(newNumCarryOn);
}
//Calculate age
public int calculateAge(int currentYear){
int age = 0;
if (currentYear < birthYear){
return -1;
}
else {
age = currentYear - birthYear;
}
return age;
}
//Gain weight
public void gainWeight(){
weight += 1;
}
//Gain weight based on input
public void gainWeight(double pounds){
if (weight > 0){
weight = weight + pounds;
}
}
//Get birthYear
public int getBirthYear(){
return birthYear;
}
//Get gender
public char getGender(){
return gender;
}
//Get name
public String getName(){
return name;
}
//Get weight
public double getWeight(){
return weight;
}
//Get numCarryOn
public int getNumCarryOn(){
return numCarryOn;
}
//isFemale
public boolean isFemale(){
if (gender == 'f'){
return true;
}
else {
return false;
}
}
//isMale
public boolean isMale(){
if (gender == 'm'){
return true;
}
else {
return false;
}
}
//loseWeight
public void loseWeight(){
if (weight > 0){
weight -= 1;
}
}
//lose weight by value
public void loseWeight(double weight2lose){
if (weight - weight2lose >= 0){
weight -= weight2lose;
}
}
//print
public void printDetails(){
System.out.printf("Name: %20s | Year of Birth: %4d | Weight: %10.2f | Gender: %c | NumCarryOn: %2d\n", name, birthYear, weight, gender, numCarryOn);
}
//setGender
public void setGender(char newGender){
if (newGender == 'f' || newGender == 'm') {
this.gender = newGender;
} else {
this.gender = 'u';
}
}
//setName
public void setName(String newName){
name = newName;
}
//setBirthYear
public void setBirthYear(int newBY){
birthYear = newBY;
}
//setWeight
public void setWeight(double newWeight){
if (newWeight < 0){
weight = -1;
}
else {
weight = newWeight;
}
}
//setNumCarryOn
public void setNumCarryOn(int newNumCarryOn){
if (newNumCarryOn < 0){
numCarryOn = 0;
}
else if (newNumCarryOn > 2){
numCarryOn = 2;
}
else {
numCarryOn = newNumCarryOn;
}
}
}
Airplane Class
import java.util.Arrays;
public class Airplane {
private Passenger[] passengers;
private String airplaneName;
private int numPassengers;
public int count = 0;
//default constructor
public Airplane(){
airplaneName = "";
passengers = new Passenger[100];
numPassengers = 0;
}
//default constructor with String input
public Airplane(String otherairplaneName){
airplaneName = otherairplaneName;
passengers = new Passenger[100];
numPassengers = 0;
}
//default constructor with int input
public Airplane(int otherArraySize){
airplaneName = "";
if (otherArraySize < 0){
otherArraySize = 0;
passengers = new Passenger[otherArraySize];
}
numPassengers = 0;
}
//default constructor with String and int input
public Airplane(String otherAirplaneName, int otherArraySize){
airplaneName = otherAirplaneName;
if (otherArraySize < 0){
otherArraySize = 0;
passengers = new Passenger[otherArraySize];
}
numPassengers = 0;
}
//add a passenger
public void addPassenger(Passenger a){
for (int i = 0; i < passengers.length; i++){
if (passengers.length - count >= 0){
passengers[i] = a;
break;
}
}
}
}
Driver
public class RunAirplane {
public static void main(String[] args) {
Airplane a1 = new Airplane("Flight 1", 100);
Passenger p1 = new Passenger("Albert", 1879, 198.5, 'm', 2);
Passenger p2 = new Passenger("Grace", 1906, 105, 'f', 1);
Passenger p3 = new Passenger("Tim", 1955, 216.3, 'm', 2);
Passenger p4 = new Passenger("Steve", 1955, 160, 'm', 2);
Passenger p5 = new Passenger("Sergey", 1973, 165.35, 'm', 1);
a1.addPassenger(p1);
a1.count += 1;
a1.addPassenger(p2);
a1.count += 1;
a1.addPassenger(p3);
a1.count += 1;
a1.addPassenger(p4);
a1.count += 1;
a1.addPassenger(p5);
a1.count += 1;
a1.printAllDetails();
}
}
You are trying to access the global variable passengers which is getting initialized only when the parameter otherArraySize is smaller than 0 as specified in the constructor that you are using. Try reversing the condition for that parameter. This way your array would initialize.
public Airplane(String otherAirplaneName, int otherArraySize){
airplaneName = otherAirplaneName;
if (otherArraySize > 0){
otherArraySize = 0;
passengers = new Passenger[otherArraySize];
}
numPassengers = 0;
}
import java.util.*;
class Distance {
private String name;
private int dist;
public Distance(String name, int dist) {
this.name = name;
this.dist = dist;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDist() {
return dist;
}
public void setDist(int dist) {
this.dist = dist;
}
public String toString() {
return "Distance [name=" + name + ", school street=" + dist + "]";
}
}
class DistanceComp {
public static Distance longdistance(Distance[] dim) {
Distance max = dim[0];
for (int i = 1; i < dim.length; i++) {
if (max.getDist() < dim[i].getDist())
max = dim[i];
}
return max;
}
public static Distance shortdistance(Distance[] dim) {
Distance min = dim[0];
for (int i = 1; i < dim.length; i++) {
if (min.getDist() > dim[i].getDist())
min = dim[i];
}
return min;
}
}
public class week03_01 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Distance[] dist = new Distance[3];
System.out.print(">> how many students? : ");
int num = in.nextInt();
for (int i = 0; i < num; i++) {
System.out.print(">> name and distance : ");
dist[i] = new Distance(in.next(), in.nextInt());
}
System.out.println("\na student with the longest commute to school : " + DistanceComp.longdistance(dist));
System.out.println("a student with the shortest commute to school : " + DistanceComp.shortdistance(dist));
System.out.println("school distance difference is " + );
}
}
I also want to print the "shool distance differecnce".
but it doesn't calculate. i think it has String types.
I think calculate only integer types in an array, but i don't know the code.
Or is s there any other way? Ask for advice.
In your DistanceComp class, create a method to subtract two Distances similar like you did for longdistance and shortdistance:
public static int subtractDistance(Distance dist1, Distance dist2) {
int difference = Math.abs(dist1.getDist() - dist2.getDist());
return difference;
}
Then, use that in your System.out:
System.out.println("school distance difference is " + DistanceComp.subtractDistance(DistanceComp.longdistance(dist), DistanceComp.shortdistance(dist)));
Some notes fyi:
Your code currently only works with 3 students.
Instead of using long names, assign them to a shorter-named variable. This helps with code readability.
As part of the curriculum at my school, we are working on some CodeHS Java.
There is one problem that I'm stuck on:
Taking our Student and Classroom example from earlier, you should fill in the method getMostImprovedStudent, as well as the method getExamRange. The most improved student is the one with the largest exam score range.
To compute the exam score range, you must subtract the minimum exam score from the maximum exam score.
For example, if the exam scores were 90, 75, and 84, the range would be 90 - 75 = 15.
This is the Student class which I added my method getExamRange().
import java.util.*;
public class Student
{
private static final int NUM_EXAMS = 4;
private String firstName;
private String lastName;
private int gradeLevel;
private double gpa;
private int[] exams;
private int numExamsTaken;
public static int[] examRange = new int[Classroom.numStudentsAdded];
private int i = 0;
/**
* This is a constructor. A constructor is a method
* that creates an object -- it creates an instance
* of the class. What that means is it takes the input
* parameters and sets the instance variables (or fields)
* to the proper values.
*
* Check out StudentTester.java for an example of how to use
* this constructor.
*/
public Student(String fName, String lName, int grade)
{
firstName = fName;
lastName = lName;
gradeLevel = grade;
exams = new int[NUM_EXAMS];
numExamsTaken = 0;
}
public int getExamRange()
{
Arrays.sort(exams);
examRange[i] = exams[exams.length-1] - exams[0];
i++;
return exams[exams.length-1] - exams[0];
}
public String getName()
{
return firstName + " " + lastName;
}
public void addExamScore(int score)
{
exams[numExamsTaken] = score;
numExamsTaken++;
}
// This is a setter method to set the GPA for the Student.
public void setGPA(double theGPA)
{
gpa = theGPA;
}
/**
* This is a toString for the Student class. It returns a String
* representation of the object, which includes the fields
* in that object.
*/
public String toString()
{
return firstName + " " + lastName + " is in grade: " + gradeLevel;
}
}
And this is the Classroom class in which I added the method getMostImprovedStudent().
import java.util.*;
public class Classroom
{
Student[] students;
static int numStudentsAdded;
public Classroom(int numStudents)
{
students = new Student[numStudents];
numStudentsAdded = 0;
}
public Student getMostImprovedStudent()
{
Arrays.sort(Student.examRange);
//return Student.examRange[0];
}
public void addStudent(Student s)
{
students[numStudentsAdded] = s;
numStudentsAdded++;
}
public void printStudents()
{
for(int i = 0; i < numStudentsAdded; i++)
{
System.out.println(students[i]);
}
}
}
I can get the exam Range by sorting the exams array then subtracting the smallest from the biggest, but once I do this, how do I find the student with the biggest exam range, and return it?
The way you would do this is looping through students, and have a variable to hold the biggest difference in score, and the most improved student:
public Student getMostImprovedStudent()
{
Student mostImproved = students[0];
int biggest = student[i].getExamRange();
for(int i = 1; i < students.length; i++) {
if(students[i].getExamRange() > biggest) {
mostImproved = students[i];
biggest = students[i].getExamRange();
}
}
return mostImproved;
}
However Java 8+ we can do:
public Student getMostImprovedStudent()
{
return Arrays.stream(students)
.max(Comparator.comparing(Student::getExamRange))
.get();
}
Which is assuming that students is not empty
As I explained in the comment above you can do it this way:
public Student getMostImprovedStudent() {
Student maxRangeStudent = null;
int maxRange = 0;
for (Student student: students) {
int curExamRange = student.getExamRange();
if (curExamRange > maxRange){
maxRangeStudent = student;
maxRange = curExamRange;
}
}
return maxRangeStudent;
}
I am a novice java student. I have a project for a java class where I am to write a Golfer class, Score class and a tester for the Golfer class to test all methods. The specific problems I am having is :
When I call the addScore method, the method overwrites the old data instead of adding to the existing. I need to get the program to add the scores in the array in addition to the previous score.
The findScore method is private and used in the public method getScore, however, when I run the program, I get the null value regardless of the parameter in the call. I need to return the index of an array based on the date entered.
The following is excerpts from the code and not the entire program.
public class Golfer {
/**String representing the golfer's name*/
private String name;
/**String representing the golf course where the golfer's hadicap is kept*/
private String homeCourse;
/**Unique integer that identifies every golfer*/
private int idNum;
/**Array storing all the golfer's scores*/
private Score[] scores;
public static int nextIDNum;
/**Default constructor, sets all instance field to a default value. Creates Array.
*/
public Golfer() {
name = "";
homeCourse = "";
scores = new Score[0];
nextIDNum = 1000;
}
/**Constructor sets name and homeCourse from parameters and uses the static variable nextIDNum to retrieve the next available ID number. Creates Array.
*/
public Golfer(String golferName, String home) {
setName(golferName);
setHomeCourse(home);
setNextIDNum(nextIDNum);
scores = new Score[10];
}
/**Creates a Score object from the parameters that represent the course, course rating, course slope, date and score. Adds the newly created Score object to the Array of Scores.
#param golfCourse A String representing the golf course name
#param rating A double representing the golf course rating
#param slope An int representing the golf course slope
#param scoreDate A String representing the date the course was played
#param score An int representing what was scored on the course
*/
public void addScore(String golfCourse, double rating, int slope, String scoreDate, int score) {
Score[] golfScores = new Score[scores.length + 1];
for (int i = 0; i < scores.length; i++) {
golfScores[i] = scores[i];
}
golfScores[golfScores.length - 1] = new Score(golfCourse, rating, slope, scoreDate, score);
scores = golfScores;
}
/**Deletes a score from the Array based on score date, Assumes only one score per day.
#param golfDate A string representing the date of the golf score
#return true if score found and deleted,
#return false if score not found.
*/
public boolean deleteScore (String golfDate) {
for (int i = 0; i < scores.length; i++) {
if (findScore(golfDate) > 0) {
scores[i] = null;
}
return true;
}
return false;
}
/**Returns a score object based on the score date. If not found returns null
#param golfDate The date of the golf score
#return Scores[i] The score on the parameterized date
#return null A null value if the score was not found.
*/
public Score getScore(String golfDate) {
for (int i = 0; i < scores.length; i++) {
if (findScore(golfDate) > 0) {
return scores[i];
}
}
return null;
}
/**Given a parameter representing the score's date, finds the score on a given date and returns the Array index of a score. Return constant NOTFOUND if not found.
#param golfDate A string representing the date of the score
#return i An array index representing the score
#return NOTFOUND A constant set to -1 if the score isn't found
*/
private int findScore(String golfDate) {
final int NOTFOUND = -1;
for (int i = 0; i < scores.length; i++) {
if (scores[i].equals(golfDate)) {
return i;
}
}
return NOTFOUND;
}
}
The score class:
public class Score {
private String courseName;
private int score;
private String date;
private double courseRating;
private int courseSlope;
public Score(String course, double rating, int slope, String golfDate, int scr) {
setCourseName(course);
setScore(scr);
setDate(golfDate);
setCourseRating(rating);
setCourseSlope(slope);
}
public Score() {
courseName = "";
score = 0;
date = "";
courseRating = 0.0;
courseSlope = 0;
}
public void setCourseName(String course) {
courseName = course;
}
public String getCourseName() {
return courseName;
}
public void setScore(int golfScore) {
if ((golfScore < 40) && (golfScore > 200)) {
golfScore = 9999;
System.out.println("Error: golf score must be between 40 and 200.");
}
score = golfScore;
}
public int getScore() {
return score;
}
public void setDate(String golfDate) {
date = golfDate;
}
public String getDate() {
return date;
}
public void setCourseRating(double rating) {
if ((rating < 60) && (rating > 80)) {
rating = 9999;
System.out.println("Error: the course rating must be between 60 and 80.");
}
courseRating = rating;
}
public double getCourseRating() {
return courseRating;
}
public void setCourseSlope(int slope) {
if ((slope < 55) && (slope > 155)) {
slope = 9999;
System.out.println("Error: The course slope must be between 55 and 155.");
}
courseSlope = slope;
}
}
The tester class:
public class GolferTester {
public static void main (String []args) {
String course1 = "Augusta National";
String course2 = "Bayhill CC";
String course3 = "TPC Sawgrass";
String player1 = "Sam Snead";
String player2 = "Arnold Palmer";
String player3 = "Jack Nicklaus";
int score1 = 66;
int score2 = 201;
int score3 = 72;
int slope1 = 60;
int slope2 = 156;
int slope3 = 77;
double rating1 = 65.2;
double rating2 = 81.8;
double rating3 = 70.9;
String date1 = "01/01/2017";
String date2 = "06/01/2016";
String date3 = "12/22/2016";
Golfer golfer1 = new Golfer(player1, course1);
Golfer golfer2 = new Golfer(player2, course2);
Score s1 = new Score(course1, rating1, slope1, date1, score1);
Score s2 = new Score(course2, rating2, slope2, date2, score2);
s1.setScore(score1);
s1.setDate(date1);
s1.setCourseRating(rating1);
s1.setCourseSlope(slope1);
s1.setCourseName(course1);
s2.setScore(score3);
s2.setDate(date3);
s2.setCourseRating(rating3);
s2.setCourseSlope(slope3);
s2.setCourseName(course3);
golfer1.addScore(s1.getCourseName(), s1.getCourseRating(), s1.getCourseSlope(), s1.getDate(), s1.getScore());
golfer2.addScore(s2.getCourseName(), s2.getCourseRating(), s2.getCourseSlope(), s2.getDate(), s2.getScore());
System.out.println(golfer1);
System.out.println("");
System.out.println(golfer2);
System.out.println("");
s1.setScore(score2);
s1.setCourseRating(rating2);
s1.setCourseSlope(slope2);
golfer1.addScore(s1.getCourseName(), s1.getCourseRating(), s1.getCourseSlope(), s1.getDate(), s1.getScore());
System.out.println(golfer1);
deleteScore(s1.getDate());
System.out.println(s1.getDate());
}
}
Any help would be appreacited
Don't use an array for a list of elements that you know will grow/shrink. Use ArrayList, which has add()
Your getScore method is returning null always
I have a 2D Array called sectionArray which is of type Student class. Each student has a name and an array[] of 5 grades for exam scores. These names and scores are divided into to sections in a file that must be read. I keep getting a nullPointer on my sectionArray no matter what I change. Thank you for any suggestions.
import java.util.*;
import java.io.*;
public class ProgressReport {
private Student[][] sectionArray;// each student has a name,
// grade, average,
// and the array of scores
private File file;
private Scanner inputFile;
public ProgressReport() throws IOException {
sectionArray = new Student[2][];
file = new File("Lab5A.in.txt");
inputFile = new Scanner(file);
}
/**
*
* #return the values in the sectionArray
*/
public Student[][] getSectionArray() {
return sectionArray;
}
/**
* initialize sectionArray and set the values
*
* #param sectionArray
* passed 2D array of Students
*/
public void setSectionArray(Student[][] sectionArray) {
for (int i = 0; i < sectionArray.length; i++) {
for (int j = 0; j < sectionArray[i].length; j++) {
this.sectionArray[i][j] = sectionArray[i][j];
}
}
}
/**
* reads from the file and creates new Students
*
* #throws IOException
*/
public void readInputFile() throws IOException {
int colNum = 0;
int section = 0;
String name = " ";
int[] grades = new int[5];
while (inputFile.hasNext()) {
colNum = inputFile.nextInt();// gets size of row
sectionArray[section] = new Student[colNum];// initialize array
// iterates through colNum amount of times
for (int j = 0; j < colNum; j++) {
name = inputFile.next();// gets next name in column
for (int i = 0; i < 5; i++) {
// stores scores for that name
grades[i] = inputFile.nextInt();
}
// creates new Student with name and grades
sectionArray[section][j] = new Student(name, grades);
section++;
}
}
// passes the values in sectionArray
setSectionArray(sectionArray);
}
}
My student class looks like this:
public class Student {
private String name = " "; // Store the name of the student
private char grade; // Store the letter grade
private double average; // Store the average score
private int[] scores; // Store the five exam scores
public Student() {
grade = ' ';
average = 0.0;
}
public Student(String name, int[] score) {
this.name = name;
scores = new int[5];
for (int i = 0; i < scores.length; i++) {
scores[i] = 0;
}
for (int i = 0; i < scores.length; i++) {
this.scores[i] = score[i];
}
}
// getters
public String getName() {
return name;
}
public char getGrade() {
return grade;
}
public double getAverage() {
return average;
}
// think about changing this to return a different format
public int[] getScores() {
return scores;
}
// setters
public void setScore(int[] scores) {
}
public void setName(String name) {
this.name = name;
}
public void setGrade(char grade) {
this.grade = grade;
}
public void setAverage(double average) {
this.average = average;
}
/**
* determine the average of the five test scores for each student
*/
public void calculateAverage() {
double total = 0;
double average = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
average = total / scores.length;
setAverage(average);
}
/**
* Determine the student's letter grade based on average of test scores
*/
public void calculateGrade() {
double average = 0;
average = getAverage();
if (average <= 100 && average >= 90) {
setGrade('A');
} else if (average <= 89 && average >= 80) {
setGrade('B');
} else if (average <= 79 && average >= 70) {
setGrade('C');
} else if (average <= 69 && average >= 60) {
setGrade('D');
} else if (average <= 59 && average >= 0) {
setGrade('F');
}
}
public String toString() {
return getName() + " " + getAverage() + " " + getGrade();
}
}
The array you are trying to write is initialized with sectionArray = new Student[2][];. This will create a matrix (2D array) with 2 columns and only one row, and then you try to set your new values on this array. If you already know the size of the matrix you are going to read from the file, then initialize it with the correct values.
Anyway, I did not get why you are trying to use a 2D array for this. If I understood correctly the purpose of your code, you should be using a List instead to store the read data, and since it has a dynamically increasing size you wouldn't have to bother with controlling the indexes like you do with the array. Take a look at this tutorial to learn how to use lists.