0

I have my PhoneBook program but I am trying to get the entries to automatically be put in alphabetical order when the user enter "l" for the list of entries. I can't figure out though how to do that. I've tried putting it into a list a sorting it that way but it only takes the first name of the entry.

import java.io.*;
import java.util.*;
class Entry {
    public String fname, number, note, lname;
}
public class Main {
    public static Entry[] contactList;
    public static int num_entries;
    public static Scanner stdin = new Scanner(System.in);
    public static void main(String args[]) throws Exception{
        int i; char C;
        String code, Command;
        contactList = new Entry[200];
        num_entries = 0;
        readPhoneBook("PhoneBook.txt");
        System.out.println("Please Enter A Command.\nUse" +
                " \"e\" for enter," +
                " \"f\" for find," +
                " \"l\" for listing all the entries," +
                " \"m\" to merge duplicate entries," +
                " \"d\" to delete an entry," +
                " \"q\" to quit.");
        Command = null;
        C = ' ';
        while(C != 'q'){
            System.out.print("Command: ");
            Command = stdin.next();
            C = Command.charAt(0);
            switch (C) {
                case 'e': addContact(); break;
                case 'f':
                    code = stdin.next();
                    stdin.nextLine();
                    i = index(code);
                    if (i >= 0) displayContact(contactList[i]);
                    else System.out.println("**No entry with code " + code); break;
                case 'l':
                    listAllContacts(); break;
                case 'q':
                    CopyPhoneBookToFile("PhoneBook1.txt");
                    System.out.println("Quitting the application. All the entries are "
                            + "stored in the file PhoneBook1.txt"); break;
                case 'm':

                    break;
                case 'd':

                    break;
                default:
                    System.out.println("Invalid command Please enter the command again");
            }
        }
    }
    public static void readPhoneBook(String FileName) throws Exception {
        File F;
        F = new File(FileName);
        Scanner S = new Scanner(F);
        while (S.hasNextLine()) {
            contactList[num_entries]= new Entry();
            contactList[num_entries].fname = S.next();
            contactList[num_entries].lname = S.next();
            contactList[num_entries].number = S.next();
            contactList[num_entries].note = S.nextLine();
            num_entries++;
        }
        S.close();
    }
    public static void addContact() {
        System.out.print("Enter First Name: ");
        String fname = stdin.next(); //First Name
        stdin.nextLine();
        System.out.print("Enter Last Name: ");
        String lname = stdin.next(); //Last Name
        String number;
        stdin.nextLine();
        contactList[num_entries] = new Entry();
        contactList[num_entries].fname = fname; //Saves first name as fname
        contactList[num_entries].lname = lname; //Saves last name as lname
        System.out.print("Enter Number: ");
        number = stdin.nextLine();
        contactList[num_entries].number = number; //Saves phone number as number
        System.out.print("Enter Notes: ");
        contactList[num_entries].note = stdin.nextLine(); //saves any notes
        num_entries++;
    }
    public static int index(String Key) {
// Function to get the index of a key from an array
// if not found, returns -1
        for (int i=0; i < num_entries; i++) {
            if (contactList[i].fname.equalsIgnoreCase(Key))
                return i; // Found the Key, return index.
        }
        return -1;
    }
    public static void displayContact(Entry contact) {
        System.out.println("--"+ contact.fname+"\t"+
                contact.lname+"\t"+
                contact.number+"\t"+
                contact.note);
    }
    public static void listAllContacts() {
        int i = 0;
        while (i < num_entries) {
            displayContact(contactList[i]);
            i++;
        }
    }
    public static void CopyPhoneBookToFile(String FileName) throws Exception{
        FileOutputStream out = new FileOutputStream(FileName);
        PrintStream P = new PrintStream( out );
        for (int i=0; i < num_entries; i++) {
            P.println(contactList[i].fname + "\t" + contactList[i].lname + "\t" + contactList[i].number +
                    "\t" + contactList[i].note);
        }
    }}

this is the bit for entering a new contact

public static void addContact() {
        System.out.print("Enter First Name: ");
        String fname = stdin.next(); //First Name
        stdin.nextLine();
        System.out.print("Enter Last Name: ");
        String lname = stdin.next(); //Last Name
        String number;
        stdin.nextLine();
        contactList[num_entries] = new Entry();
        contactList[num_entries].fname = fname; //Saves first name as fname
        contactList[num_entries].lname = lname; //Saves last name as lname
        System.out.print("Enter Number: ");
        number = stdin.nextLine();
        contactList[num_entries].number = number; //Saves phone number as number
        System.out.print("Enter Notes: ");
        contactList[num_entries].note = stdin.nextLine(); //saves any notes
        num_entries++;
    }
John231
  • 3
  • 2
  • 1
    That's a lot of code for someone to read through. Can you narrow down the problem to a shorter block of code? – chevybow Apr 30 '19 at 18:44
  • Possible duplicate of [Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?](https://stackoverflow.com/questions/4903611/java-list-sorting-is-there-a-way-to-keep-a-list-permantly-sorted-automatically) – admlz635 Apr 30 '19 at 20:09

1 Answers1

1

I would make a compareTo method in your Entry class and set it up to compare how you want it to. The most important part would be how you store your Entries though. If you use a sorted array, you can just sort everything as you enter it into the array, and blast through linearly when you want to read it out

Grimmpier
  • 74
  • 1
  • 9