So basically I have to import a file of names and then be able to sort them alphabetically I've managed to import the file using the followig code
import java.io.*;
public class Name{
public static void main(String[] args)throws IOException{
String contents;
File f = new File("names.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while (br.ready()){
contents = br.readLine();
System.out.println(contents);
}
fr.close();
}
}
also I have managed to sort them alphabetically when manually entering the list of names as shown in the code below
public class DSA1
{
public static void main(String[ ] args)
{
String[ ] names = {"James", "Anne", "Bill", "Maria", "Bob", "Jill", "Elvis", "Carol", "Dennis", "Mandy", "Steven", "Sian", "Harry", "Linda"};
sortStringExchange (names);
for ( int k = 0; k < 14; k++ )
System.out.println( names [ k ] );
}
public static void sortStringExchange( String x [ ] )
{
int i, j;
String temp;
for ( i = 0; i < x.length - 1; i++ )
{
for ( j = i + 1; j < x.length; j++ )
{
if ( x [ i ].compareToIgnoreCase( x [ j ] ) > 0 )
{ // ascending sort
temp = x [ i ];
x [ i ] = x [ j ]; // swapping
x [ j ] = temp;
}
}
}
}
}
I just can't seem to figure out how to combine the two pieces of code to make one program, all help will be much appreciated