0

My program calls for being able to delete a name and number from a phone book. I have gotten the deleting to work but it only deletes the index from the ArrayList that the text box corresponds to. I need to be able to delete both the name and the number from their respective arraylists by entering it into either texbox. Sorry if there is another answer to this i guess i dont really know how to word it correctly. My code is below.

ArrayList<String> Names = new ArrayList<String>();
ArrayList<String> Numbers = new ArrayList<String>();

if(e.getSource() == DeleteBtn)
{


if (NameTxt.getText() != null)
{
for( int i=0; i<= NamesList.size(); i++)
{
if(NamesList.contains(NameTxt.getText()))
{
NamesList.remove(i);
System.out.println(NamesList.size());

}                                                    
}
}

if (PhoneTxt.getText() != null)
{
for( int i=0; i<= NumbersList.size(); i++)
{
if(NumbersList.contains(PhoneTxt.getText()))
{
NumbersList.remove(i);
System.out.println(NumbersList.size());

}                                                    
}
}
}
  • 1
    Are the names directly mapped to the numbers? In that case you should use a different data structure. Hint... the names are `mapped` – Kon Mar 03 '15 at 17:40
  • what do you mean by the names being mapped to the numbers? – Austin Makovi Mar 03 '15 at 17:42
  • To anyone concerned, I MUST use ArrayLists for the project – Austin Makovi Mar 03 '15 at 17:45
  • 1
    If you're forced to use an ArrayList instead of a Map, consider creating a PhoneContact object that contains both the name and number. Then you'll only have 1 ArrayList and you'll have similar functionality to that of the map. – Ryan D Mar 03 '15 at 17:53

6 Answers6

2

If you HAVE to use ArrayLists then you might consider using an ArrayList of pairs. You can create your own class, say PhoneBookEntry.

class PhoneBookEntry {
    String _name;
    String _phone;
    // etc...
}

ArrayList<PhoneBookEntry>
1

Consider using a HashMap instead of the ArrayLists.

HashMap<String, String> numbersAndNames = new HashMap<String, String>();
numbersAndNames.put("John", "123 456 789");
thekiwi5000
  • 137
  • 7
René Winkler
  • 6,508
  • 7
  • 42
  • 69
  • first off i'd up vote this if i had the rep and also unfortunately i cant use anything else but Array Lists for this project, but i do really like this answer – Austin Makovi Mar 03 '15 at 17:52
0

If I were you, I would reconsider my logic. I'm going to try and avoid posting code for you to keep from giving you the answer/doing the work for you but...

  1. I only see you modifying the NamesList variable. You aren't modifying the Names or Numbers ArrayList variables.

  2. I would put your 'contains if statement' some place else. Your checking to see if NameList contains NameText a bunch of times. I don't see why you would need to check it more than once.

  3. If you want to get smart, you can do away with the array completely and just use the getIndex() method to some effect in java...which gets the index corresponding to NameText (I'm being vague here deliberately, so think about what I'm saying).

  4. You can use a HashMap if you want, but it's not necessary.

mljohns89
  • 887
  • 1
  • 11
  • 16
0

Per suggestion of using an additional class to track the name/number combo.

ArrayList contacts = new ArrayList();

if(e.getSource() == DeleteBtn) {

   if (NameTxt.getText() != null) {
      for( int i=0; i<= contacts.size(); i++) {
         if(contacts.getName().contains(NameTxt.getText())) {
           contacts.remove(i);
           System.out.println(contacts.size());

      }                                                    
   }
}

public class PhoneContact {
   private String name;
   private String number;
   /*
      Getters and Setters
   */
 }
Ryan D
  • 1,023
  • 11
  • 16
0

First of all, NumbersList.contains(PhoneTxt.getText()) returns if PhoneTxt.getText() is anywhere in the list.

What you want to check is the NumbersList.get(i).equals(PhoneTxt.getText()) note that i used equals() instead of == operator

thekiwi5000
  • 137
  • 7
0

my friend actually found the answer, he simply added the other ArrayList(i), thank you to all who posted answers, as they gave me food for thought, just thought the logic of the answer would not work but i was proven wrong, here is the code for anyone interested.

if(e.getSource() == DeleteBtn)
{


if (NameTxt.getText() != null)
{
for( int i=0; i<= NamesList.size(); i++)
{
if(NamesList.contains(NameTxt.getText()))
{
NamesList.remove(i);
NamesList.remove(i);

System.out.println(NamesList.size());
System.out.println(NumbersList.size());

}                                                    
}
}

if (PhoneTxt.getText() != null)
{
for( int i=0; i<= NumbersList.size(); i++)
{
if(NumbersList.contains(PhoneTxt.getText()))
{
NumbersList.remove(i);
NamesList.remove(i);

System.out.println(NamesList.size());
System.out.println(NumbersList.size());

}                                                    
}
}
}