sorting a loaded string array
hi. i have loaded a file from disk but need to sort out the contents and i'm having trouble with that. pls excuse the messy code as i keep changing stuff trying to make it work.
import java.util.*;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;
publicclass coursework4
{
//Contact details array
privatestaticfinalint MAX_RECORDS = 20;
privatestatic String lastName[] =new String[MAX_RECORDS];
privatestatic String firstName[] =new String[MAX_RECORDS];
privatestatic String telNumber[] =new String[MAX_RECORDS];
privatestatic String emailAddress[] =new String[MAX_RECORDS];
privatestatic Scanner data_input =new Scanner(System.in);
publicstaticint read_in_file(String file_name)
{
Scanner read_in;
Scanner line;
int record_count = 0;
String record;
try
{
read_in =new Scanner(new File(file_name));
// read in one line at a time
read_in.useDelimiter(System.getProperty("line.separator"));
while (read_in.hasNext())
{
// Test to see if there are too many records in the file
if (record_count == MAX_RECORDS)
{
System.out.printf("Only %d records allowed in file", MAX_RECORDS);
System.exit(0);
}
// read in record
record =new String(read_in.next());
// Split the record up into its fields and store in
// appropriate arrays
line =new Scanner(record);
line.useDelimiter("\\s*,,\\s*");
lastName[record_count] = line.next();
firstName[record_count] = line.next();
telNumber[record_count] = line.next();
emailAddress[record_count] = line.next();
// Increment record count
record_count++;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return record_count;
}
publicstaticvoid write_out_file(int no_of_records, String filename)
{
PrintStream write_out;
int counter;
try
{
// Create new file
write_out =new PrintStream(new File(filename));
// Output all reacords
for(counter = 0; counter < no_of_records; counter++)
{
// Output a record
write_out.print(lastName[counter]);
write_out.print(",,");
write_out.print(firstName[counter]);
write_out.print(",,");
write_out.print(telNumber[counter]);
write_out.print(",,");
write_out.println(emailAddress[counter]);
}
// Close file
write_out.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
// Your 'Methods' go here
//Start the application and display the main menu allowing the user to make a menu choice
publicstaticvoid menu_display()
{
int dot = 0;
System.out.printf("%nStarting Application. Please Wait...");
//if (dot < 100)
//{
//for (int r = 0; r < 20000000; ++ r) System.currentTimeMillis ();
//{
//System.out.print("..");
//}
//for (int r = 0; r < 20000000; ++ r) System.currentTimeMillis ();
//{
//System.out.print(".....................");
//}
//for (int r = 0; r < 5000000; ++ r) System.currentTimeMillis ();
//{
//System.out.print(".....................");
//}
//for( int i = 0; i < 25; i++ )
//{
//System.out.println();
//}
//}
System.out.printf("%n Contacts Application Menu!%n");
System.out.printf("-%n");
System.out.printf("Enter 1 To Search For A Contact. (* Lists ALL!).%n");
System.out.printf("Enter 2 For Inserting A Contact.%n");
System.out.printf("Enter 3 For Removing A Contact. %n");
System.out.printf("Enter 4 For Saving Before Quit.%n");
System.out.printf("Enter 5 To Quit Without Saving.%n");
System.out.printf("-%n");
System.out.print("Please Enter An Option Now: ");
}
//This method is called when the user wishes to display all records loaded from the masterfile
publicstaticvoid display_all()
{
for (int d = 0; d < 20; d++)
{
if (lastName[d] !=null)
{
System.out.printf("%n%s%n", lastName[d]);
System.out.printf("%s%n", firstName[d]);
System.out.printf("%s%n", telNumber[d]);
System.out.printf("%s%n", emailAddress[d]);
}
}
}
publicstaticvoid main(String[] args)
{
// Declare Variables
Scanner dataInput =new Scanner(System.in);
int number_of_records, records, n, sort, choice;
String filename, quit, first_name, last_name, enter;
String tel_num =" ";
String email_addr =" ";
// Get the masterfile filename, read in the contents of the masterfile and sort them
System.out.print("Enter The Masterfile File Name: ");
filename = dataInput.next();
//System.out.printf("%nMasterfile Loading!%nPlease Wait...");
//for (int r = 0; r < 35000000; ++ r) System.currentTimeMillis ();
//{
//System.out.print("..");
//}
//for (int r = 1000000; r < 10000000; ++ r) System.currentTimeMillis ();
//{
//System.out.printf("............%n%n");
//}
// Your 'main' code goes here
//Display main menu and get option input from user
{
menu_display();
choice = dataInput.nextInt();
int lower_boundary = 1;
int upper_boundary = 5;
while ((choice < lower_boundary) || (choice > upper_boundary))
{
System.out.printf("Invalid option! Please re-enter.%n");
menu_display();
choice = dataInput.nextInt();
}
if (choice == 1)
{
System.out.printf(" ");
System.out.print("Please Enter Last Name: ");
last_name = dataInput.next();
System.out.print("Do You with to Enter First Name? (Y/N): ");
enter = dataInput.next();
if (enter.equalsIgnoreCase("N"))
{
for (int i = 0; i < number_of_records; i++)
{
if (last_name.compareTo(lastName[i]) == 0)
{
System.out.printf("%n%s%n", lastName[i]);
System.out.printf("%s%n", firstName[i]);
System.out.printf("%s%n", telNumber[i]);
System.out.printf("%s%n", emailAddress[i]);
}
}
}
if (enter.equalsIgnoreCase("Y"))
{
System.out.print("Please Enter First Name: ");
first_name = dataInput.next();
for (int i = 0; i < number_of_records; i++)
{
if ((last_name.compareTo(lastName[i]) == 0) && (first_name.compareTo(firstName[i]) == 0))
{
System.out.printf("%n%s%n", lastName[i]);
System.out.printf("%s%n", firstName[i]);
System.out.printf("%s%n", telNumber[i]);
System.out.printf("%s%n", emailAddress[i]);
}
}
}
if (last_name.compareTo("*") == 0)
{
for (int i = 0; i < number_of_records; i++)
//There cannot be more then a 20 data records in the file
{
if (lastName[i] !=null)
{
System.out.printf("%n%s%n", lastName[i]);
System.out.printf("%s%n", firstName[i]);
System.out.printf("%s%n", telNumber[i]);
System.out.printf("%s%n", emailAddress[i]);
}
}
}
}
if (choice == 2)
{
}
if (choice == 3)
{
}
if (choice == 4)
{
// Get new filename and write out the file
System.out.print("Please Enter New Masterfile File Name: ");
filename = dataInput.next();
write_out_file(number_of_records, filename);
System.out.print("File Save In Progress. Please Wait...");
for (int r = 0; r < 35000000; ++ r) System.currentTimeMillis ();
{
System.out.print("..");
}
for (int r = 1000000; r < 10000000; ++ r) System.currentTimeMillis ();
{
System.out.print("............");
}
System.out.printf("%nGoodbye!");
}
if (choice == 5)
//Quit without saving option active
{
System.out.printf("Caution!%n");
System.out.print("Data May Be Lost. Do You Wish To Proceed? (Y/N): ");
quit = dataInput.next();
if (quit.compareTo("N") == 0)
{
menu_display();
choice = dataInput.nextInt();
}
elseif (quit.compareTo("Y") == 0);
{
System.out.printf("Goodbye!");
}
}
}
}
}

