Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java that asks for user input for the names of 5 people and t

ID: 667357 • Letter: W

Question

Write a program in Java that asks for user input for the names of 5 people and their ages. Then in the display list the 5 students in order by age first and name second.

This is my code so far...It keeps throwing a java.util.InputMisMatchException..can anyone look over my code and help me?

this is the code for the main method

public class StudentDemo

{

public static void main(String[] args) {

List<Student> studentList = new ArrayList<Student>(5);

// Scanner accepts input from the user

Scanner keyboard = new Scanner(System.in);

for (int i =1; i<6 ;i++){

System.out.println("Enter student "+i+" name:");

String name = keyboard.next();

System.out.println("Enter student "+i+" age:");

int age = keyboard.nextInt();

Student student = new Student(name,age);

// Each student object is created and added to the student list

studentList.add(student);

}

/**

* Collections.sort() is a method provided by Java that sorts the objects in the list

* based on comparator. The sorting algorithm is used here is modified merge sort.

*/

Collections.sort(studentList,new Comparator<Student>() {

@Override

public int compare(Student student, Student student1) {

return student.getAge() - student1.getAge();

}

});

System.out.println("The sorted student list: ");

for (int i=0; i< studentList.size();i++) {

System.out.println(studentList.get(i).getName());

}

}

}

and the code for the student class

public class Student

{

private String name;

private int age;

public Student(String name, int age)

{

this.name = name;

this.age = age;

}

public String getName()

{

return name;

}

public int getAge()

{

return age;

}

}

Explanation / Answer

import java.util.*;
import java.io.*;

class Student implements Comparator<Student>{
private String name;
private int age;
public Student(){}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int compare(Student s1,Student s2) {
       return (s1.getAge() - s2.getAge());
   }
}

class StudentDemo{
   public static void main (String[] args){
       ArrayList<Student> studentList = new ArrayList<Student>(5);  
       Scanner keyboard = new Scanner(System.in);
       for (int i =1; i<6 ;i++){
System.out.println("Enter student "+i+" name:");
String name = keyboard.next();
System.out.println("Enter student "+i+" age:");
int age = keyboard.nextInt();
Student student = new Student(name,age);
// Each student object is created and added to the student list
studentList.add(student);
}
Collections.sort(studentList,new Student());
for (int i = 0; i < 6; i++){
           Student std = studentList.get(i);
           System.out.println(std.getAge()+" "+std.getName());
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote