i need to sort by last name

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

public class TestStudent 
{
	public static void main(String[] args) 
    {
   
//Course Code     
   String [] courseNumber = {"MTH151","MTH201","ITF215","ENG101","HIS210","BIO120"};
//Course Name                                                
   String [] courseName = {"Statistics","Calculus I","Programming in Java",-"Composition","US History","Biology "};
//Credit                                      
   int [] credit = {3,4,4,3,3,4};
   
   
   ArrayList <Student> student = new ArrayList <Student>();  
   
   for(int x = 0; x < 3; ++x)
   {  
      student.add(getInfo(courseNumber,courseName,credit) );   
   }
   
   System.out.println("-------------------------------------");   
   display(student);
   
   } 
   
   public static Student getInfo(String[] courseNum,String[] courseName,int[] credit)
   { 
       Scanner scan = new Scanner(System.in);
	   System.out.print("\nFirst Name: ");
	   String firstName = scan.next(); 
	     
	   System.out.print("Last Name: ");
	   String lastName = scan.next();
	   
	   System.out.print("Corse Number: ");
	   String corseNumber = scan.next();
	   
	   String fullName = (lastName + ", " + firstName); 
	   
	   
	   int result = -1;
	   do{
	   
		   result = searchCourseNumber(courseNum,corseNumber);   	
		   if(result == -1)
		   {
		   		System.out.print("Invalid Corse Number");
		   		System.out.print("Corse Number: ");
		   		corseNumber = scan.next();
		   	}
	   }while(result == -1);
	   
	   return new Student(fullName,courseName[result], courseNum[result],credit[result]);    
   }   
   
   public static int searchCourseNumber(String[] classes, String key)
   {
   	for(int i = 0; i < classes.length; i++)
   		if(key.compareTo(classes[i]) == 0)
   			return i;
   	
   	return -1;
   	
   }
   
   public static void sortThePersonnel(ArrayList <Student> stu)
   { 
  

   
   
   
   }
   
   public static void display(ArrayList <Student> stu)
   {
   for(Student temp: stu)
       System.out.println(temp.toString());   
       

   }
}
 class Student 
{
//Static Variable	
	 int costPerHour = 105;
	 
//Instance Variables
     private String name;
	 private String	courseTitle;
	 private String	courseNumber;
	 private double	courseFee;
	 
//Default Constructor
	 public Student()
	 {	 		
	 }
	 
//Constructor
	 public Student(String studentName,String CTitle,String CNumber,double CFee)
	 {
	 	name = studentName;
	 	courseTitle = CTitle;
	 	courseNumber = CNumber;
	 	courseFee = CFee*costPerHour;	 	
	 }
	 
//Getters 
	 public String getName()
	 {
	 	return name;
	 }
	 public String getCourseTitle()
	 {
	 	return courseTitle;
	 }
	 public String getCourseNumber()
	 {
	 	return courseNumber; 
	 }
	 public double getCourseFee()
	 {
	 	return courseFee;
	 }
	 
//Setters  
	 public void setName(String StuName)
	 {
	 	name = StuName;
	 }
	 public void setCourseTitle(String title)
	 {
	 	courseTitle =title;
	 }
	 public void setCourseNumber(String number)
	 {
	 	courseNumber = number;
	 }
	 public void setCourseFee(double fee)
	 {	 	
	 	courseFee = fee;
	 }
	 
//toString method
     public String toString()
	{
			
		String print = ("\nName: " + getName() + "\nCourse: " + getCourseTitle() + "\nCourse Number: " 
			+ getCourseNumber() + "\nCourse Fee: " + getCourseFee());				
		return print;
	 }
}

Recommended Answers

All 3 Replies

So what's stopping you? (you haven't asked a question here)

So what's stopping you? (you haven't asked a question here)

i don't know how to sort an arrayList

Do you know how to sort an Array? If so, use ArrayList.toArray, and then sort the Array. You'll have to manually iterate through the array to turn it back into an ArrayList, but that's easy.

You should probably look at the Comparable interface in order to make your Student class sortable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.