I have added a for loop in size of 3 it should scan for 3 times but it is scanning for 4 time.for size of 4 its scanning for 6 times help me out.
public class Control {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Student> studs = new ArrayList<Student>();
for (int i = 1;i < 3;i++) { //Here the for loop
System.out.println("Enter age and name :");
Student stu1 = new Student(sc.nextInt(),sc.nextLine());
System.out.println("Enter age and name :");
Student stu2 = new Student(sc.nextInt(),sc.nextLine());
studs.add(stu1);
studs.add(stu2);
Collections.sort(studs,new StudAge());
}
for(Student stud : studs) {
System.out.println(stud);
}
}
}