murrmurr
murrmurr

Reputation: 115

How to display arraylist in tabular form in the java console

I want to display the data of the arraylist student in tabular form. I have this code but it only displays the last set of data I input in the table. what is wrong with my code?

public class gradesummary extends student{

    static ArrayList<student> studentList = new ArrayList<student>();

    @SuppressWarnings("resource")
    public static void main(String[] args){
        student student = new student();

        Scanner in = new Scanner(System.in);

        for (int x=0; x<=10; x++){
            System.out.print("Student Name: ");
            try {
                    student.name = in.nextLine();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Section: ");
            try {
                    student.section = in.nextLine();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Midterm Grade: ");
            try {
                    student.mgrade = in.nextDouble();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Second Quarter Grade: ");
            try {
                    student.sqgrade = in.nextDouble();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Final Exam Grade: ");
            try {
                    student.fegrade = in.nextDouble();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 

            student.fgrade = (student.mgrade*1/5)+(student.sqgrade*3/10)+(student.fegrade*9/20);
            studentList.add(student);
            in.nextLine();
        }
        Show(studentList);
    }

    private static void Show(ArrayList<student> studentList2) {
        new student();
        System.out.println("               Name           Section          Midterm Grade          Second Quarter Grade         Final Exam Grade         Final Grade     ");
        for (int i=0;i<=10;i++)
        {
            student student1 = studentList.get(i);
            System.out.println ("                      " +student1.name+ "          "+student1.section+"           "+student1.mgrade+"         "+student1.sqgrade+"        "+student1.fegrade+"       "+student1.fgrade+" " );
        }  
    }              
}

this is the my class student

public class student {

String name;
String section;
Double mgrade;
Double sqgrade;
Double fegrade;
Double fgrade;
}

How can I make the table data straight? more organize?

How can I make the table data straight? more organize? like this: enter image description here

Upvotes: 0

Views: 3241

Answers (2)

GreenOnBlack
GreenOnBlack

Reputation: 66

You only create one student and you always overwrite that students data. You must create a new student after you had all inputs

    student.fgrade = (student.mgrade*1/5)+(student.sqgrade*3/10)+(student.fegrade*9/20);
    studentList.add(student);
    student = new student();
    in.nextLine();

Also remove new student() from Show. I would also recommend to use another variable name.

Upvotes: 1

Salah
Salah

Reputation: 8657

You have declared the student object just one time, so it will take the last values inserted because your are using the same student object.

try to move the declaration of student inside loop like this:

for (int x=0; x<=10; x++){
    student student = new student();
    //rest of your code
}

Upvotes: 1

Related Questions