A few questions about java

  • #26
BiGyElLoWhAt
Gold Member
1,573
118
This seems strange. I just realized that I was being careful enough with my code (while typing out my logic on here) that I don't actually need the try catch block, assuming the file always comes in the correct format (which I will only ever have to read this one file). I got rid of the try catch to try to simplify the program a bit. I also deleted a lot of things to try to clean it up. I think this is peculiar, but hopefully someone can show my why it's not.
Java:
import java.io.*;
import java.util.*;

public class ProgressReport{   
   
    File file = new File("grades.txt");   
    Student[][] student;  //An array of student objects, the one we care about
    Student[][] add;  //A secondary array, used for adding length to the student array (in number of students
    Student[][] col;  //A preemptive strike, presumably I will need a third array for columns, currently unused
    int[] sec = {0,0}; //number of students per section   
    int[] scores = {0,0,0,0,0};  // Scores for a section
    String name = "";  //Name of the student
   
   
    public ProgressReport() throws FileNotFoundException{
        student = new Student[1][1];  //initialization
        readInputFile();
        generateReport();       
    }
   
   
   
    private void readInputFile() throws FileNotFoundException{
        @SuppressWarnings("resource")
        Scanner s = new Scanner(file);  //used to get the next line from the file
        Scanner line; //used to get the next token on a line
          
        int[] moar; // used to add sections to sec
        int k = 0; //index of student within a section
        int j = -1; //index of sec that carries the number of students
        String score;
       
        while(s.hasNextLine())
        {
            String str = s.nextLine(); //get next line
           
            if(str.length() <= 3)
            {
                j++; //move to the next section
                k=0; //set student index to zero (first student in the class)
                if(j>2)
                {
                    moar = new int[sec.length+1];
                    for(int ii = 0;ii<sec.length+1;ii++){
                        moar[ii] = sec[ii]; // add length and copy array
                    }
                    sec = moar.clone();
                }
                sec[j] = Integer.parseInt(str);  // turn the string value of the number of students to an int
                student[j] = new Student[sec[j]]; //create a column of length sec[j]
                add = new Student[student.length +1][student[j].length]; //add length and copy
                for(int jj = 0; jj<=j; jj++){
                    for(int kk = 0 ; kk<=k;kk++){
                        add[jj][kk]= student[jj][kk];
                    }
                }
                student = add.clone();               
            }
           
            else{
                line = new Scanner(str);
                while(line.hasNext())
                {                   
                    name = line.next();
                    for(int i =0; i<scores.length ;i++)
                    {   
                        score = line.next(); //set name equal to the next token
                        scores[i] = Integer.parseInt(score); //check if name is an int
                    }
                    student[j][k] = new Student(name, scores);
                   
                   
                    System.out.println(student[j][k].getName());
                    System.out.println(student[j][k].getAverage());
                    System.out.println(student[j][k].getGrade());
                   
                    System.out.println(j+","+k);
                    k++;                   
                }               
            }           
        }
       
    }
   
   
   
    private void generateReport(){       
        System.out.println("Progress Report \n");
        for(int i = 0 ;i<=sec.length;i++)
        {
            System.out.println("Section " +(i+1));
            for(int j = 0; j<=sec[i];j++)
            {
                System.out.println(student[i][j].getName() + " " + student[i][j].getAverage() + " " + student[i][j].getGrade());               
            }
        }
    }
}

I get this output:
Code:
Johnson
90.6
A
0,0
Aniston
81.2
B
0,1
Cooper
82.2
B
0,2
Gupta
72.2
C
0,3
Blair
52.2
F
0,4
Clark
59.2
F
1,0
Kennedy
63.4
D
1,1
Bronson
90.0
A
1,2
Sunny
84.8
B
1,3
Smith
75.4
C
1,4
Diana
68.8
D
1,5
Progress Report

Section 1
Johnson 90.6 A
Exception in thread "main" java.lang.NullPointerException
    at ProgressReport.generateReport(ProgressReport.java:117)
    at ProgressReport.readInputFile(ProgressReport.java:103)
    at ProgressReport.<init>(ProgressReport.java:18)
    at Lab1A.main(Lab1A.java:6)
Everything works greatly as I'm filling the array initially. I get everything exactly as it should be, including the indices of the student. However, I am getting null pointer exception when I try to access students[0][1].getName(). Not 0,0 . Only for this 0,1. However, if you look up, I am filling students[0][1] with a student with name Aniston, average 81.2, grade B.
 
  • #27
BiGyElLoWhAt
Gold Member
1,573
118
.... It's filling properly, but somewhere I'm overriding (0,1)-(0,5) :frown:
via the variable explorer and some breakpoints...
 
  • #28
BiGyElLoWhAt
Gold Member
1,573
118
Ok, I found it. I was resetting k to zero, and then cloning, for kk < k, so switching to kk <sec[jj] fixed my issue.
 
  • #29
BiGyElLoWhAt
Gold Member
1,573
118
On a somewhat unrelated note, does anyone know how I can swap the position of "outline" and "variables" in eclipse Neon? Currently, outline appears on the rhs, and variables likes to be on the top. I would rather have variables on the side, so I can minimize outline and have my actual code be large vertically, on the LHS and my variables large on the RHS.
 

Related Threads on A few questions about java

  • Last Post
Replies
4
Views
1K
Replies
6
Views
712
Replies
6
Views
3K
V
Replies
2
Views
4K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
5
Views
4K
Replies
1
Views
1K
Replies
2
Views
10K
Top