-1

I have to find the highest scoring student and the lowest scoring student from the given user input.but i only get the highest scoring student and can't get the lowest scoring student from it.


public class Student{

    public String name;
    public String id;
    public int score;
    public static int n;

    public Student(String initName,String initID,int initScore){

        initName=name;
        initID=id;
        initScore=score;
    }

    public Student (){

    }



    public static void main(String[] args) {

        System.out.println("Enter the number of students:");
        Scanner s1=new Scanner(System.in);

      
        Student.n=Integer.parseInt(s1.nextLine().trim());

        System.out.println("Enter the student name,id and score.");
        Scanner s2=new Scanner(System.in);

        Student st1=new Student();
        Student min=new Student(" "," ",100);
        Student max=new Student(" "," ",0);
        for(int i=0;i<Student.n;i++){
            st1.name=s2.next();
            st1.id=s2.next();
            st1.score=s2.nextInt();

            if(max.score<st1.score){
                max.score=st1.score;
                max.name=st1.name;
                max.id=st1.id;
            }
            if(min.score>st1.score){
                min.name=st1.name;
                min.score=st1.score;
                min.id=st1.id;
            }
        }
        System.out.println("the highest scoring student: "+max.name);
        System.out.println("the lowest scoring student: "+min.name);
        
       
    }
}
nobalG
  • 4,544
  • 3
  • 34
  • 72
  • 2
    Please do not use nonsense characters to just add length to your text. The limits are there to force you to explain your problem and code. – NomadMaker Feb 01 '21 at 18:16
  • https://stackoverflow.com/questions/31083405/how-to-get-minimum-and-maximum-value-from-list-of-objects-using-java-8 – Ivan Barayev Feb 01 '21 at 18:17

3 Answers3

0
        initScore=score;

Please fix the above line in constructor.

Actually all the assignments are incorrect:

    public Student(String initName,String initID,int initScore){

        initName=name;
        initID=id;
        initScore=score;
    }

It should be changed to

    public Student(String initName, String initId, int initScore) {
        name = initName;
        id = initId;
        score = initScore;
    }

Even better

    public Student(String name, String id, int score) {
        this.name = name;
        this.id = id;
        this.score = score;
    }

Since score is not assigned in the initial construction, it is assigned a default value of 0 and hence impacts the min computation. (as its already at min assuming the scores are positive)

It works for max computation as the scores obtained will be greater than or equal to 0 and will eventually update the score directly(assuming only positive scores are allowed).

Thiyanesh
  • 2,360
  • 1
  • 4
  • 11
0

I see plenty of problems in your code and it seems you don't really know how to code, which makes me wanna recommend going through some Java tutorial first.

Now to your code:

  • Student class implementation is just full of flaws, no encapsulation, mistakes in constructor, no idea what n is used for

    public class Student {
    
        private String name;
        private String id;
        private int score;
    
        public Student(String initName, String initID, int initScore){
            this.name = initName;
            this.id = initID;
            this.score = initScore;
        }
    
        // declare getters such as these two
        public String getName() {
            return this.name;
        }
    
        public int getScore() {
            return this.score;
        }
    }
    
  • your main method has so many mistakes too: you have 2 Scanners, you don't initialize enough students and your iteration is plain wrong

    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        // get number of students
        System.out.println("Enter the number of students:");
        int numberOfStudents = s1.nextInt();
        // initialize array of students
        Student[] array = new Student[numberOfStudents];
        for(int i = 0; i < numberOfStudents; i++) {
            // get name of the student
            System.out.println("Enter the student name:");
            String name = s1.nextLine();
            // get id of student
            System.out.println("Enter the student id:");
            String id = s1.nextLine();
            // get score of student
            System.out.println("Enter the student score:");
            int score = s1.nextInt();
            array[i] = new Student(name, id, score);
        }
        // now you have students input
        // it's time to find your student
        // set the lowest and highest student to first student
        Student min = array[0];
        Student max = array[0];
        for(int i = 1; i < numberOfStudents; i++) {
            if(min.getScore() > array[i]) {
                min = array[i];
            } else if(max.getScore() < array[i]) {
                max = array[i];
            }
        }
        System.out.println("the highest scoring student: "+max.getName());
        System.out.println("the lowest scoring student: "+min.getName());
    }
    
AP11
  • 617
  • 4
  • 11
0

Carefully check the code again :

    public Student(String initName,String initID,int initScore){

    initName=name;
    initID=id;
    initScore=score;
}

Did you get it...? Buggy constructor. The code should be

    public Student(String initName,String initID,int initScore){

    name = initName;
    id = initID;
    score = initScore;
}
Lasith M
  • 48
  • 7