-3

When I run the option to sort the results, the first 2 are in correct form but the remaining are just the copy of the 2nd detail. The other functions run fairly smoothly, the only problem I am facing is with the sort() function. I have given it much thoughts to no avail.

import java.util.ArrayList;
import java.util.Scanner;

class Test {

    public static int totaln=0;
    public static ArrayList<String> fname;
    public static ArrayList<String> lname;
    public static ArrayList<String> dob;
    public static ArrayList<String> gender;
    public static ArrayList<Integer> standard;
    public static ArrayList<Integer> rollno;
    public static ArrayList<Float> maths;
    public static ArrayList<Float> science;
    public static ArrayList<Float> social;
    public static ArrayList<Float> eng;
    public static ArrayList<Float> percentage ;

     public static void entry() {

        int num;

        Scanner sc=new Scanner(System.in);
         fname= new ArrayList<>();
         lname= new ArrayList<>();
         dob= new ArrayList<>();
         gender= new ArrayList<>();
         standard= new ArrayList<>();
         rollno= new ArrayList<>();
         maths =new ArrayList<>();
         science = new ArrayList<>();
         social = new ArrayList<>();
         eng = new ArrayList<>();
         percentage = new ArrayList<>();

        System.out.print("Enter the number of students: ");
        num = sc.nextInt();
        if(num==0 || num<0){
            System.out.println("Invalid Entry.");
        }
           else {
                for (int i = 0; i < num; i++) {
                    System.out.println();
                    System.out.println("Student: " +(i+1));
                    System.out.print("Enter the standard: ");
                    standard.add(sc.nextInt());
                    System.out.print("Enter the first name: ");
                    fname.add(sc.next());
                    System.out.print("Enter the last name: ");
                    lname.add(sc.next());
                    System.out.print("Enter the roll no: ");
                    rollno.add(sc.nextInt());
                    System.out.print("Enter the date of birth: ");
                    dob.add(sc.next());
                    System.out.print("Enter the gender: ");
                    gender.add(sc.next());
                    System.out.print("Enter the marks in Maths: ");
                    maths.add(sc.nextFloat());
                    System.out.print("Enter the marks in Science: ");
                    science.add(sc.nextFloat());
                    System.out.print("Enter the marks in Social: ");
                    social.add(sc.nextFloat());
                    System.out.print("Enter the marks in English: ");
                    eng.add(sc.nextFloat());
                    percentage.add((maths.get(i)+ science.get(i)+ social.get(i)+ eng.get(i))/4);
                }
                totaln+=num;

            }


    }
    public static void display(){
        if(totaln==0){
            System.out.println("No data has been entered.");
        }
        else{
            System.out.println();
            System.out.println("Student Details");
            for (int i=0;i<totaln;i++){
                System.out.println();
                System.out.println("Name " +fname.get(i)+" "+lname.get(i));
                System.out.println("Standard: "+standard.get(i));
                System.out.println("Roll no: " +rollno.get(i));
                System.out.println("Date of birth: " +dob.get(i));
                System.out.println("Gender: " +gender.get(i));
                System.out.println("Percentage: " +percentage.get(i));
            }

        }

    }
    public static void search(){
        int roll;
        Scanner sc= new Scanner(System.in);
        if(totaln==0){
            System.out.println("No data has been entered.");
        }
        else{
            System.out.println();
            System.out.println("Enter the roll no of the student to search: ");
            roll = sc.nextInt();
            int s=0;

            for(int i=0;i<totaln;i++){
                if(roll==rollno.get(i)){
                    s=s+1;
                    System.out.println();
                    System.out.println("Name " +fname.get(i)+" "+lname.get(i));
                    System.out.println("Standard: "+standard.get(i));
                    System.out.println("Roll no: " +rollno.get(i));
                    System.out.println("Maths: "+maths.get(i));
                    System.out.println("Science: "+science.get(i));
                    System.out.println("Social: "+social.get(i));
                    System.out.println("English: "+eng.get(i));
                    System.out.println("Percentage: " +percentage.get(i));

                }

            }
            if(s>0) {
                System.out.println("Invalid entry. ");
            }
        }

    }
    public static void sort(){
        if(totaln==0){
            System.out.println("No data has been entered.");
        }
        else{
            int c= percentage.size();
            for (int i=0; i<c-1; i++)
            {
                int pos = i;
                for (int j=i+1; j<c; j++)
                {
                    if (percentage.get(j) > percentage.get(pos))
                    {
                        pos = j;
                    }
                }
                float t1 = percentage.get(pos);
                percentage.add(pos, percentage.get(i)) ;
                percentage.add(i,t1);

                String f = fname.get(pos);
                fname.add(pos,fname.get(i));
                fname.add(i,f);

                String l = lname.get(pos);
                lname.add(pos,lname.get(i));
                lname.add(i,l);
            }
            System.out.println("The required order: ");

            for (int i=0;i<3;i++){
                System.out.println("Rank: " +(i+1));
                System.out.println("Percentage: "+percentage.get(i));
                System.out.println("Name: " +fname.get(i)+" "+lname.get(i));
            }



        }
    }

    public static void delete(){
        int roll;
        Scanner sc= new Scanner(System.in);
        if(totaln==0){
            System.out.println("No data has been entered.");
        }
        else{
            int choice;
            System.out.println("Enter 1 to delete all record:");
            System.out.println("Enter 2 to delete the record of a student");
            choice= sc.nextInt();
            if(choice==1){
                totaln=0;
                System.out.println("All the records have been deleted.");

            }
            else if(choice==2){
                System.out.println();
                System.out.println("Enter the roll no of the student you miss delete the record of: ");
                roll = sc.nextInt();

                for(int i=0;i<totaln;i++){
                    if(roll==rollno.get(i)){
                        for(int j=i;j<totaln;j++){
                            standard.remove(j);
                            fname.remove(j);
                            lname.remove(j);
                            rollno.remove(j);
                            dob.remove(j);
                            gender.remove(j);
                            percentage.remove(j);
                        }
                        totaln--;
                        System.out.println("The record has been deleted.");

                    }
                }

            }
            else {
                System.out.println("Invalid input.");
            }

        }

    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int value;
        char ch;
        do {
            System.out.println("Enter 1 to enter data.");
            System.out.println("Enter 2 to show data.");
            System.out.println("Enter 3 to search data.");
            System.out.println("Enter 4 to view the result in descending order: ");
            System.out.println("Enter 5 to delete data.");
            System.out.println("Enter 6 to exit.");
            System.out.println();
            System.out.print("Enter the value: ");
            value=sc.nextInt();
            switch(value){
                case 1:
                    entry();
                    break;
                case 2:
                    display();
                    break;
                case 3:
                    search();
                    break;
                case 4:
                    sort();
                    break;
                case 5:
                    delete();
                    break;
                case 6:
                    break;
                default:
                    System.out.println("Invalid input.");
                    break;
            }
            System.out.println("Do you want to continue(y/n): ");
            ch=sc.next().charAt(0);
        }while(ch=='y' || ch=='Y');
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Usually to avoid that kind of problems we create single class which will hold information about single student like one shown in answer below. This way we can have single `List`. Then we can sort such list using data in each student. See [Sort ArrayList of custom Objects by property](https://stackoverflow.com/q/2784514) or [Sorting an ArrayList of objects using a custom sorting order](https://stackoverflow.com/q/1814095) – Pshemo Feb 13 '22 at 13:36

3 Answers3

2

This is not a very good solution, you shouldn't use that many ArrayLists. Try creating a new Student Class, that has all the fields that you want, like first and last name, Birthday, gender, etc.

Then you could go through that process without using that many ArrayLists and just use student.setAge(sc.next())

Instead of having this many lists you would just have 1 list with all the student objects in it. You also don't need the totaln variable since you can use arraylist.length

Here is an example Student class

public class Student {
    
    private String fName;
    private String lName;
    private int age;

    public Student() {}

    public Student(String fName, String lName, int age) {
        this.fName = fName;
        this.lName = lName;
        this.age = age;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}
Brentspine
  • 274
  • 1
  • 15
0

Instead of using different arraylist for each property, create an object of Student

class Student implements Comparable<Student>{
String name = "";
String gender = "";
Double percentage = 0.0;

Student( String name,
String gender ,
Double percentage){
    this.name = name;
    this.gender = gender;
    this.percentage = percentage;
}

@Override
public int compareTo(Student s) {
     int firstPercentage = (int) Math.round(this.percentage);
      int secondPercentage = (int) Math.round(s.percentage);
    return  firstPercentage - secondPercentage;
}

}

and then create list of object like this

  Student s1 = new Student("student1", "male",  10.0);
 Student s2 = new Student("student2", "male",  20.0);
 Student s3 = new Student("student3", "female", 30.0);
 Student s4 = new Student("student4", "male",  40.0);
 Student s5 = new Student("student5", "female",  90.0);
 Student s6 = new Student("student6", "male",  80.0);

 
ArrayList<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);  
students.add(s3); 
students.add(s4); 
students.add(s5);  
students.add(s6);

first sort them using collections, here sort and reverse will work as per percentage because you've implemented Comparable in Student class which is comparing object on the basis of percentage in compareTo() method.

Collections.sort(students);

and then reverse the list by collections

Collections.reverse(students);

and then you have list of students in descending order with respect to percentage

 for(Student i : students){
        System.out.println(i.percentage);
}
Huma
  • 11
  • 3
0

I would suggest a step by step solution:

first create a class of Student and implement a Comparable then override the compareTo method as follows:

public class Student implements Comparable<Student>{
    
    private String fName;
    private String lName;
    private int age;
    private double percentage;

    public Student() {}

    public Student(String fName, String lName, int age, double percentage) {
        this.fName = fName;
        this.lName = lName;
        this.age = age;
        this.percentage = percentage;
    }

    public String getfName() {
        return fName;
    }

    public String getlName() {
        return lName;
    }

    public int getAge() {
        return age;
    }

    public double getPercentage(){return percentage;}

    @Override
    public int compareTo(Student o) {
        if (percentage == o.percentage){
            return 0;
        } else if(percentage > o.percentage){
            return 1;
        }else{
            return -1;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "fName='" + fName + '\'' +
                ", lName='" + lName + '\'' +
                ", age=" + age +
                ", percentage=" + percentage +
                '}';
    }
}

Second in your main method create objects of Student and store them inside an ArrayList:

public class StudentRun{
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<Student>(){
            {
                add(new Student("Bob","Logan", 24, 78.9));
                add(new Student("Mike","Staats", 22, 90.4));
                add(new Student("Robert", "Finley", 44, 85.3));
                add(new Student("Hector", "Kalona", 35, 73.8));
            }
        };

        Collections.sort(students);
        System.out.println(students);

    }
}

I would suggest to have a look at Java Collections and Comparable interface.

Rafa
  • 467
  • 4
  • 18