Some help with java programming

Associate
Joined
10 Oct 2005
Posts
283
Location
Liverpool, UK
ok, so i have a small project to work on, using java to make a small application that reads from an external data file.

i need help sorting out a menu system and some operations for the application to run, as i am terrible at programming!

so far my code will read the file when you type in the name, then letyou save it with a new name.

next i need to work on getting the application to display the information (name, phone number, email).

after i do this, i will move onto stuff like getting it to order the information, adding and deleting information and saving it.

if you cant offer direct help, links to websites are very welcome.

thanks
 
i understand roughly what your saying guys, but i really havent a clue on how to implement it.

i got a ton of help from my mate, and my code thus far is this:

Code:
import java.util.*;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class contacts
{
    private static final int MAX_RECORDS = 20;
    private static String lastName[] = new String[MAX_RECORDS];
    private static String firstName[] = new String[MAX_RECORDS];
    private static String telNumber[] = new String[MAX_RECORDS];
    private static String emailAddress[] = new String[MAX_RECORDS];
    private static Scanner data_input = new Scanner(System.in);
    
    public static int 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;
    }
    
    public static void 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
    public static void main(String[] args)
    {   
        
        // Declare Variables
        int user_choice;
        boolean quit = false;
        Scanner dataInput = new Scanner(System.in);
        int number_of_records, records, n, sort;
        String filename, first_name, last_name, enter, tel_num, email_addr;
        
        // Get filename and read in file
        System.out.print("Enter the masterfile file name: ");
        filename = data_input.next();
        number_of_records = read_in_file(filename);
        
        // Your 'main' code goes here

        //display menu
        do
        {
        System.out.printf("%n%nMenu%n%n");
        System.out.println("1. Search records (use * to list all)");
        System.out.println("2. Insert record");
        System.out.println("3. Delete record");
        System.out.println("4. Quit with saving");
        System.out.println("5. Quit without saving");

        //get users choice
        user_choice = data_input.nextInt();

        //process choice
        switch(user_choice)
        {
            case 1:
               	{
					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)
								//Arrays.sort(lastName[i]);
							{
								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] != last_name)
							{
								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]		);
							}
						}
					}
				}
                    break;
            case 2:
               {
                System.out.print("Please enter the last name of the contact: ");
                    last_name = dataInput.next();
                    System.out.print("Please enter the first name of the contact: ");
                    first_name = dataInput.next();
                    System.out.print("Please enter the telephone number of the contact: ");
                    tel_num = dataInput.next();
                    System.out.print("Please enter the email address of the contact: ");
                    email_addr = dataInput.next();
                    int z = 0;
                    for (int i=0; i < number_of_records; i++)
                    {
                        if ((last_name.compareTo(lastName[i])) < i && (last_name.compareTo(lastName[i]) > i))
                        {
                            z = i;
                        }
                    }
                    for (int i = number_of_records; i > z; i--)
                    {
                        lastName[i] = lastName[i-1];
                    }
                    lastName[z+1] = last_name;
                    firstName[z+1] = first_name;
                    telNumber[z+1] = tel_num;
                    emailAddress[z+1] = email_addr;
                }
                    break;
            case 3:
            	{
					System.out.print("Please enter the last name of the contact: ");
					last_name = dataInput.next();
					
					int z = 0;
					for (int i=0; i < number_of_records; i++)
					{
						if ((last_name.compareTo(lastName[i])) < i && (last_name.compareTo(lastName[i]) > i))
						{
							z = i;
						}
					}
					for (int i = z; i < number_of_records; i++)
					{
						lastName[i] = lastName[i+1];
					}
					
					// 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!");
				}
                    break;
            case 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!");
				}
                    break;
            case 5:
            quit = true;
                    break;
            
            default:
                    System.out.println("Invalid menu choice, please try again");
                }
            } while(!quit);

    }
}

the problem being is that the code is erronous (my mate hasnt gotten any further)

options 1, 4 and 5 work perfectly. option 3 works fine as far as i know. the problem is option 2. you can easily add a new record, e.g. Surname, Name, phone number and email address, but it doesnt add it to the end of the record file as it should do, instead it writes over the second record, and then shifts the surnames of the records below it.

could anyone please help with a solution?
 
cheers for the code protocol,im gonna give it a whizz later.

if its not too much trouble,couldyou have a look through the code i posted and try to fix the error in the functions for inserting a new record, as im trying to make it work just with the DOS text window.

thanks again =)
 
Back
Top Bottom