Results 1 to 4 of 4
  1. #1
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Removing an Item from an array

    I have a phonebook application where I need to delete an entry including the number and notes connected to that entry. When the program is first started the phonebook txt file is loaded into an array. I currently have the program setup to where it changes the name, number and notes to " " but doesn't remove it from the file.

    PHP Code:
    // Luettke, Chris -- Project 5
    import java.io.File;
    import java.io.PrintStream;
    import java.util.*;
    import javax.swing.JFrame;
    
    public class PhonebookApplication {
    	public static class Entry {
    		public String name, number, notes;
    	}
    
    	public static Entry contactList[] = new Entry[100];
    	static int num_entries = 0;
    
    	public static void readPhoneBook(String FileName) throws Exception {
    		File f = new File(FileName);
    		Entry temp = new Entry();
    		Scanner fileIn = new Scanner(f);
    
    		while (fileIn.hasNext() && num_entries < 100) {
    			temp = new Entry();
    			temp.name = fileIn.nextLine();
    			temp.number = fileIn.nextLine();
    			temp.notes = fileIn.nextLine();
    			contactList[num_entries] = temp;
    			num_entries++;
    		}
    	}
    
    	public static void storePhoneBook(String FileName) throws Exception {
    		PrintStream P = new PrintStream(FileName);
    		for (int i = 0; i < num_entries; i++) {
    			P.println(contactList[i].name);
    			P.println(contactList[i].number);
    			P.println(contactList[i].notes);
    		}
    		P.close();
    	}
    
    	public static void listAllContacts() {
    		for (int i = 0; i < num_entries; i++) {
    			System.out.println("-- " + contactList[i].name);
    			System.out.println("-- " + contactList[i].number);
    			System.out.println("-- " + contactList[i].notes + "\n");
    		}
    	}
    
    	public static void sortList() {
    		int sort, test, other;
    		Entry temp;
    		test = 0;
    		other = 1;
    		sort = 0;
    		while (sort < num_entries) {
    			for (int i = test; i < num_entries; i++) {
    				for (int j = other; j < num_entries; j++) {
    					if ((contactList[i].name).compareTo(contactList[j].name) > 0) {
    						temp = contactList[j];
    						contactList[j] = contactList[i];
    						contactList[i] = temp;
    					}
    				}
    			}
    			sort++;
    			test++;
    			other++;
    		}
    	}
    	public static void deleteEntry(String name) {
    		for (int i = 0; i < num_entries; i++) {
    			if (name.compareToIgnoreCase(contactList[i].name) == 0) {
    			contactList[i].name = "";
    			contactList[i].number = "";
    			contactList[i].notes = "";
    		}
    	}
    }
    	public static void main(String[] args) throws Exception {
    		Scanner input = new Scanner(System.in);
    		String number, notes, command, nameStr;
    		char operation;
    		operation = '\0';
    		Entry temp = new Entry();
    
    		readPhoneBook("entries.txt");
    
    		System.out.print("Codes are entered as 1 to 8 characters.\n");
    		System.out.print("Use 'e' for enter,"
    						+ " 'f' for find, 's' to sort entries, " +
    						"'l' to list entries, 'd' for delete, 'q' to quit.\n\n");
    		while (operation != 'q') {
    			System.out.print("Command: ");
    			command = input.nextLine();
    			operation = command.charAt(0);
    
    			switch (operation) {
    			case 'e':
    				storePhoneBook("entries.txt");
    				command = command.toLowerCase();
    				nameStr = command.substring(2).trim();
    
    				System.out.print("Enter number: ");
    				number = input.nextLine();
    				System.out.print("Enter notes: ");
    				notes = input.nextLine();
    
    				temp = new Entry();
    				temp.name = nameStr;
    				temp.number = number;
    				temp.notes = notes;
    				contactList[num_entries] = temp;
    				num_entries++;
    				break;
    			case 'f':
    				nameStr = command.substring(2);
    
    				for (int i = 0; i < num_entries; i++) {
    					if (nameStr == "null"){
    						break;
    					}
    					else if (nameStr.compareToIgnoreCase(contactList[i].name) == 0) {
    						System.out.println("-- " + contactList[i].name);
    						System.out.println("-- " + contactList[i].number);
    						System.out.println("-- " + contactList[i].notes + "\n");
    					}
    				}
    				break;
    			case 'l':
    				listAllContacts();
    				break;
    			case 's':
    				sortList();
    				break;
    			case 'd':
    				nameStr = command.substring(2);
    				deleteEntry(nameStr);
    				break;
    			case 'q':
    				System.out.print("\nAll Set !");
    				break;
    			default:
    				System.out.print("\nInvalid entry please enter in "
    						+ "'e','f','l','s', or 'q'" + "\n");
    				break;
    			}
    		}
    		storePhoneBook("entries.txt");
    	}
    }
    I know its not possible to delete them from the array so instead should I remove from the file?
    Last edited by chris1; 04-25-2011 at 06:30 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens if you set the value to null?

    In array named arr for number x
    Java Code:
    arr[x] = null;
    You could also consider using an arraylist, which has a remove method.

  3. #3
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Making the arr[x] = null;
    doesn't really delete the entry as it is still in the contact.txt file and is still listed.

    I haven't used an arraylist before. Would it be easy enough to implement into the program?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes, array lists are fairly easy to use. They have an underlying array, and they allow you to add as many items as you want. The underlying array is resized when you remove/add new elements.

    To use an array list you declare it like this
    Java Code:
    ArrayList<ClassName> al = new ArrayList<ClassName>();
    
    replace ClassName with your classes name, example:
    
    ArrayList<String> strings = new ArrayList<String>();
    You use the method .add(Element e) and remove(Element e) to add and remove items. For more capabilities, I suggest you view the api for it. If you are masochistic, or like re-inventing the wheel you could replace the array that stores the info in the remove method.

    Java Code:
    start delete method
      create new array of size original.length - 1
      fill with all elements except the element to be removed
      set the original array to point to the new array
    end delete method
    This is handled internally by the ArrayList class. Quick example

    Java Code:
    Element[] original = new Element[10];
    public boolean delete(Element e){
      Element[] items = new Element[original.length - 1];
      for(int i = 0; i < original.length; i++){
        if(original[i].equals(e)){
          continue;
        }
        else{
          items[i] = original[i];
        }
        return true;
      }
      return false;
    }
    There are some logic flaws here which I don't feel like working out atm. But the approach is similar to this.

Similar Threads

  1. access array list item from draw
    By trishtren in forum New To Java
    Replies: 8
    Last Post: 04-19-2011, 12:34 PM
  2. Getting an item from an array
    By ile4 in forum New To Java
    Replies: 9
    Last Post: 01-13-2011, 03:42 PM
  3. Removing indexes from an array.
    By Cruncher in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 09:41 PM
  4. Get item from string array and pack with blank space
    By firewalll in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 07:38 AM
  5. removing 0 from the begining of the array
    By Hayzam in forum New To Java
    Replies: 7
    Last Post: 08-21-2008, 10:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •