- #1
BiGyElLoWhAt
Gold Member
- 1,622
- 131
The code I'm working with
1:
How do I initialize a 2d array of objects? I keep getting crashes on a line that tries to fill an array with objects.
Student[][] student = new Student[1][1];
Student[][] add;
my plan is to use add[][] to add a row when there is a new course section found within a document and add a column when there is a new student found within the section within the document (a text file)
I keep crashing upon my first attempt to execute the following line:
student[j][k] = new Student(name, scores);
2:
Is my method an efficient way to read a text file and tokenize it? I feel like this might be the root of my error. I have a break point set on the line
student[j][k] = new Student(name, scores);
and it crashes in first iteration. Here is a little bit from my debugger.
name = "84" (id=30)
scores is an array of 0's, as expected
student -> sudent[1] -> null
I am not sure why my variable is null there. I'm assuming that it is saying i have 1 d of student arrays, and that array is null. I'm not sure why. So basically a 1x1 array with that element equal to null. I'm not sure what to do here. What am I doing incorrectly?
Java:
import java.io.*;
import java.util.*;
public class ProgressReport{
File file = new File("grades.txt");
Student[][] student = new Student[1][1];
Student[][] add;
int[] scores = {0,0,0,0,0};
String name = "";
public ProgressReport() throws FileNotFoundException{
readInputFile();
}
private void readInputFile() throws FileNotFoundException{
@SuppressWarnings("resource")
Scanner s = new Scanner(file);
Scanner line;
int[] sec = {0,0};
int k = -1;
int j = -1;
while(s.hasNextLine())
{
String str = s.nextLine();
k++;
if(str.length() == 1)
{
j++;
sec[j] = Integer.parseInt(str);
}
else{
line = new Scanner(str);
while(line.hasNext())
{
try
{
for(int i =0; ;i++)
{
scores[i] = Integer.parseInt(line.next());
}
}
catch(Exception e)
{
while(line.hasNext())
{
name = line.next();
}
}
}
student[j][k] = new Student(name, scores);
}
}
System.out.println(student);
}
}
How do I initialize a 2d array of objects? I keep getting crashes on a line that tries to fill an array with objects.
Student[][] student = new Student[1][1];
Student[][] add;
my plan is to use add[][] to add a row when there is a new course section found within a document and add a column when there is a new student found within the section within the document (a text file)
I keep crashing upon my first attempt to execute the following line:
student[j][k] = new Student(name, scores);
2:
Is my method an efficient way to read a text file and tokenize it? I feel like this might be the root of my error. I have a break point set on the line
student[j][k] = new Student(name, scores);
and it crashes in first iteration. Here is a little bit from my debugger.
name = "84" (id=30)
scores is an array of 0's, as expected
student -> sudent[1] -> null
I am not sure why my variable is null there. I'm assuming that it is saying i have 1 d of student arrays, and that array is null. I'm not sure why. So basically a 1x1 array with that element equal to null. I'm not sure what to do here. What am I doing incorrectly?