Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework delete
I am having a hard time deleting an element in an Array. I have tried for a while and I am beating my head against the desk trying to figure this out. I would greatly appreciate any help. Thanks.
 

package javaapplication9;
    import java.util.Scanner;
    import java.util.ArrayList;
    public class JavaApplication9 {
int x;
final int maxContacts = 3;
    final String[] FIRSTNAME = {"Josh", "Joe", "Jim"};
    final String[] LASTNAME = {"Jones", "Smith", "Thomas"};
    final String[] ADDRESS = {"142 Washington Ave", "500 Main St", "200 Oak Way"};
    final String[] CITY = {"Pittsburgh", "Pittsburgh", "Pittsburgh"};
    final String[] STATE = {"PA", "PA", "PA"};
    final String[] ZIP = {"15222", "15222", "15222"};
    final String[] TELEPHONE = {"412-722-1500", "412-498-2500", "412-787-3500"};
    String[] firstName = new String[3];
    String[] lastName = new String[3];
    String[] address = new String[3];
    String[] city = new String[3];
    String[] state = new String[3];
    String[] zip = new String[3];
    String[] telephone = new String[3];
String nameSearch;
boolean firstNameFound = true, lastNameFound = true, addressFound = true, cityFound = true, stateFound = true, zipFound = true, telephoneFound = true;
Scanner keyboard = new Scanner(System.in);
public void getInfo() {
        while (x < maxContacts) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter '1' To Add A Contact \nEnter '2' To Delete A Contact \nEnter '3' To Search For A Name \nEnter '4' To Display All Contacts \nEnter 'Q' To Quit");
        int usersChoice = input.nextInt();
        if (usersChoice == 1){
 
        System.out.print("Please enter a first name: ");
        firstName[x] = keyboard.nextLine();
        if (firstNameFound) {
            System.out.print("Please enter a last name: ");
            lastName[x] = keyboard.nextLine();
        }
                    if (lastNameFound){
                        System.out.print("Please enter an address: ");
                        address[x] = keyboard.nextLine();
                    }
                    if (addressFound){
                        System.out.print("Please enter a city: ");
                        city[x] = keyboard.nextLine();
                    }
                    if (cityFound){
                        System.out.print("Please enter a state: ");
                        state[x] = keyboard.nextLine();
                    }
                    if (stateFound){
                        System.out.print("Please enter a zip: ");
                        zip[x] = keyboard.nextLine();
                    }
                    if (zipFound){
                        System.out.print("Please enter a telephone number: ");
                        telephone[x] = keyboard.nextLine();
                    }
        }
        if (usersChoice == 4){
    System.out.println("\nList Of Contacts");
    System.out.println("------------------------------");
            for (int i = 0; i < FIRSTNAME.length; i++) {
        System.out.println("First Name: " + FIRSTNAME[i] + "\nLast Name: " + LASTNAME[i] + "\nAddress: " + ADDRESS[i]
                                + "\nCity: " + CITY[i] + "\nState: " + STATE[i] + "\nZip: " + ZIP[i] + "\nTelephone: " + TELEPHONE[i] + "\n-------------------------\n");
    }
    for (int i = 0; i < firstName.length; i++) {
        System.out.println("First Name: " + firstName[i] + "\nLast Name: " + lastName[i] + "\nAddress: " + address[i]
                                + "\nCity: " + city[i] + "\nState: " + state[i] + "\nZip: " + zip[i] + "\nTelephone: " + telephone[i]);
    }
        }
        if (usersChoice == 3){
    System.out.print("\n\nPlease enter a name to find "); // no idea how to
    // search a name and
    // display the
    // corresponding
    // number!
    nameSearch = keyboard.next();
    for (int i = 0; i < firstName.length; i++) {
        if (nameSearch.equals(firstName[i])) {
            System.out.println("The name " + firstName[i] + " was found " 
                    + "with the phone number " + firstName[i]);
        }
    }
        }
    } 
}
public static void main(String args[]) {
    JavaApplication9 show = new JavaApplication9();
    show.getInfo();
}
Posted 26-Jun-11 12:48pm
Edited 26-Jun-11 20:48pm
TorstenH.32.8K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Arrays are not good for deletion. You should better do all intermediate calculation with lists designed to allow random insertions and deletions. Use one of the classes implementing interface List<String> (or other element type), see: http://download.oracle.com/javase/6/docs/api/java/util/List.html[^].
 
When you need an array, you can also call the method toArray.
 
—SA
  Permalink  
Comments
Ciumac Sergiu at 26-Jun-11 23:49pm
   
Good call, my 5!
SAKryukov at 26-Jun-11 23:57pm
   
Thank you, Sergiu.
--SA
TorstenH. at 27-Jun-11 2:49am
   
I would give a 5 too - but sorry guys, this is a classical homework.
He/She's got to use arrays.
SAKryukov at 27-Jun-11 16:44pm
   
I disagree. I would say, homework criteria is irrelevant here. I do not help homework, I help in programming. If somebody needs whatever a stupid homework requires, that's not my problem.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

So you need to iterate through an array and find a certain value.
 
First of all you should change the structure of the code.
 
I hope this meets your (official) knowledge in class:
 
Set up an object containing all the data from 1 person: name, address, street, zip, phone, etc.
 
Afterwards you can browse through the list of users.
 
Check this tutorial: http://onjava.com/pub/a/onjava/2002/02/06/jdo1.html[^] for a close to your needs attempt.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 245
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,157
2 Maciej Los 3,774
3 Peter Leow 3,448
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 27 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100