Click here to Skip to main content
15,898,134 members

How can I delete an element in my array?

Josh Staley asked:

Open original thread
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();
}
Tags: Java

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900