0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You'll have to dynamically calcuate the size of your string, add missing empty spaces at the end to fill the string to a maxium length and then print it out
1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.