The objective of my program is to sort a file of students in order from a-z using arrays. I'm using selection sort method but for some reason my code is matching what i want. for example:
Enter name of a file of Students: student10.txt
In forward order, students are:
Sally (100)
Rico (82)
Sue (95)
Bill (43)
Rob (67)
Meng (94)
Pam (82)
Harvey (72)
Rich (45)
Debra (88)
Now sorting ...
In forward order, students are:
Debra (88)
Rich (45)
Harvey (72)
Pam (82)
Meng (94)
Rico (82)
Rob (67)
Bill (43)
Sally (100)
Sue (95)
//please help...
import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw21
{
//-----------------------------------------------------------------------
public static void main (String [] args) throws Exception
{
Scanner kb = new Scanner(System.in);
System.out.print("\nEnter name of a file of Students: ");
String filename = kb.nextLine();
Section section = new Section(filename);
System.out.println("\nIn forward order, students are:");
section.print(System.out);
System.out.println("\nNow sorting ...");
section.sort();
System.out.println("\nIn forward order, students are:");
section.print(System.out);
}
//-----------------------------------------------------------------------
} // end class Hw21
/////////////////////////////////////////////////////////////////////////
class Section
{
private Student [] a;
private int used;
private static final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
public Section ( String filename ) throws Exception
{
Scanner sc = new Scanner(new File(filename));
a = new Student[INIT_SIZE];
used = 0;
while ( sc.hasNext() )
{
Student s = Student.read(null,sc);
if ( used == a.length )
{
Student[] newA = new Student[2*a.length+1];
for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
newA[used] = s;
used++;
a = newA;
}
else
{
a[used] = s;
used++;
}
}
}
//-----------------------------------------------------------------------
public void print ( PrintStream ps )
{
for ( int i = 0 ; i < used ; i++ )
{
ps.println(a[i]);
}
}
//-----------------------------------------------------------------------
public int howMany() { return used; }
//-----------------------------------------------------------------------
public void sort ()
{
for ( int i = 0 ; i < used-1 ; i++ )
{
int minIndex = i;
String last = a[i].getName();
for(int j = i+1; j<used; j++)
{
String next = a[j].getName();
if (last.compareTo(next)> 0)
{
minIndex =j;
}
else
{
}
Student temp = a[minIndex];
a[minIndex]= a[i];
a[i] = temp;
}
}
}
//-----------------------------------------------------------------------
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
private String name;
private int grade;
//-----------------------------------------------------------------------
public Student ( String name, int grade )
{
this.name = name;
this.grade = grade;
}
//-----------------------------------------------------------------------
public String toString ()
{
return name + " (" + grade + ")";
}
//-----------------------------------------------------------------------
public String getName() { return name; }
//-----------------------------------------------------------------------
public int getGrade() { return grade; }
//-----------------------------------------------------------------------
public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
public static Student read ( PrintStream ps, Scanner sc )
{
if ( ps != null ) ps.println("Reading a Student record ...");
if ( ps != null ) ps.print("Enter the name: ");
String name = sc.nextLine();
if ( ps != null ) ps.print("Enter the grade: ");
int grade = sc.nextInt(); sc.nextLine();
return new Student(name,grade);
}
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////