How do you add objects from keyboard inputs into an array wi

How do you add objects from keyboard inputs into an array with a loop in java?

I am trying to store 5 student\'s data (name, id, test1 and test2, average, and letter grade) from my Student class into an array.

Solution

StudentTest.java


import java.util.Scanner;

public class StudentTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       Student s[] = new Student[5];
      
       for(int i=0; i<s.length; i++){
           System.out.print(\"Enter the student name: \");
           String name = scan.next();
           System.out.print(\"Enter the student id: \");
           int id = scan.nextInt();
           System.out.print(\"Enter the student exam1: \");
           int exam1 = scan.nextInt();
           System.out.print(\"Enter the student exam2: \");
           int exam2 = scan.nextInt();
           System.out.print(\"Enter the student letter grade: \");
           char letterGrade = scan.next().charAt(0);
           s[i] = new Student(id, name);
           s[i].setExam1(exam1);
           s[i].setExam1(exam2);
           s[i].setLetterGrade(letterGrade);
           s[i].setAverage((exam1+ exam2)/2);
       }
       for(int i=0; i<s.length; i++){
           System.out.println(\"Student Name: \"+s[i].getName());
           System.out.println(\"Student Id: \"+s[i].getId());
           System.out.println(\"Student Exam1: \"+s[i].getExam1());
           System.out.println(\"Student Exam2: \"+s[i].getExam2());
           System.out.println(\"Student Average: \"+s[i].getAverage());
           System.out.println(\"Student Letter Grade: \"+s[i].getLetterGrade());
           System.out.println(\"*****************************************\");
           System.out.println();
       }
   }

}

Student.java


public class Student {
   private String name;
   private int id;
   private int exam1;
   private int exam2;
   private double average;
   private char letterGrade;
   public Student(int id, String name){
       this.id = id;
       this.name = name;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public int getExam1() {
       return exam1;
   }
   public void setExam1(int exam1) {
       this.exam1 = exam1;
   }
   public int getExam2() {
       return exam2;
   }
   public void setExam2(int exam2) {
       this.exam2 = exam2;
   }
   public double getAverage() {
       return average;
   }
   public void setAverage(double average) {
       this.average = average;
   }
   public char getLetterGrade() {
       return letterGrade;
   }
   public void setLetterGrade(char letterGrade) {
       this.letterGrade = letterGrade;
   }
  
}

Output:

Enter the student name: Suresh
Enter the student id: 111
Enter the student exam1: 40
Enter the student exam2: 50
Enter the student letter grade: e
Enter the student name: Sekhar
Enter the student id: 222
Enter the student exam1: 50
Enter the student exam2: 60
Enter the student letter grade: d
Enter the student name: Anshu
Enter the student id: 333
Enter the student exam1: 60
Enter the student exam2: 70
Enter the student letter grade: c
Enter the student name: Revathi
Enter the student id: 444
Enter the student exam1: 70
Enter the student exam2: 80
Enter the student letter grade: b
Enter the student name: Satya
Enter the student id: 555
Enter the student exam1: 80
Enter the student exam2: 90
Enter the student letter grade: a
Student Name: Suresh
Student Id: 111
Student Exam1: 50
Student Exam2: 0
Student Average: 45.0
Student Letter Grade: e
*****************************************

Student Name: Sekhar
Student Id: 222
Student Exam1: 60
Student Exam2: 0
Student Average: 55.0
Student Letter Grade: d
*****************************************

Student Name: Anshu
Student Id: 333
Student Exam1: 70
Student Exam2: 0
Student Average: 65.0
Student Letter Grade: c
*****************************************

Student Name: Revathi
Student Id: 444
Student Exam1: 80
Student Exam2: 0
Student Average: 75.0
Student Letter Grade: b
*****************************************

Student Name: Satya
Student Id: 555
Student Exam1: 90
Student Exam2: 0
Student Average: 85.0
Student Letter Grade: a
*****************************************

How do you add objects from keyboard inputs into an array with a loop in java? I am trying to store 5 student\'s data (name, id, test1 and test2, average, and l
How do you add objects from keyboard inputs into an array with a loop in java? I am trying to store 5 student\'s data (name, id, test1 and test2, average, and l
How do you add objects from keyboard inputs into an array with a loop in java? I am trying to store 5 student\'s data (name, id, test1 and test2, average, and l

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site