javaarraysuser-defined-data-types

how to prompt user to input array values and create a data type in java


I am working on a java code where it should prompt the user to input the grades of N amount of students and it should output the highest grade, My code is working well but I want to give the user ability to enter the content of the array and I want to create a data type called Student for example for the array.

public class Test3 {
   static double grades[] = {85, 99.9, 78, 90, 98};
       
   static double largest() {
      int i;
      double max = grades[0]; 
      for (i = 1; i < grades.length; i++)
         if (grades[i] > max)
            max = grades[i];
        
      return max;
   }
   public static void main(String[] args) 
   {
      System.out.println("Highest grade is :  " + largest());
   }
}

Any help would be really appreciated.


Solution

  • First, you should create the Student class:

        public class Student {
    
            private double grade;
    
            // empty constructor
            public Student() {}
            
            // all args constructor
            public Student(double grade) {
                this.grade = grade;
            }
    
            public double getGrade() {
                return grade;
            }
    
            public void setGrade(double grade) {
                this.grade = grade;
            }
    
            @Override
            public String toString() {
                return "Student{" +
                        "grade=" + grade +
                        '}';
            }
        }
    

    To avoid repetitive code, you can use the Lombok library. It creates for you the constructors, getters, setters, toString(), and much more.

    After, you can create a method to input students grades.

        public static Student[] inputStudens() {
            Scanner sc = new Scanner(System.in);
            System.out.println("How many students?");
            int N = sc.nextInt();
    
            Student[] students = new Student[N];
            for (int i=0; i<N; i++) {
                System.out.println("What's the grade for student " + i + ":");
                double grade = sc.nextDouble();
                Student student = new Student(grade);
                students[i] = student;
            }
    
            return students;
        }
    

    And... using your already made max finder, but returning the Student instead.

        public static Student maxGrade(Student[] students) {
            int max = 0;
            for (int i = 1; i < students.length; i++) {
                if (students[i].getGrade() > students[max].getGrade()) {
                    max = i;
                }
            }
            return students[max];
        }
    

    Now we just need to call these methods in main().

        public static void main(String[] args) {
            Student[] students = inputStudens();
            Student student = maxGrade(students);
            System.out.println("Student with highest grade is: " + student);
        }