user3117238 user3117238 - 5 months ago 8
Java Question

Adding user elements multidimensional array using methods Java

The following is my problem:
"Create a program that keeps track of the following information input by the user:
First Name, Last Name, Phone Number, Age
Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns.
You should be able to add, display and remove contacts in the array.
"
**FixedThis(I'm stuck at the point of creating a method to call back and enter the users input into my 2 dimensional array.)

**New problem, I get an error for case 3, I cant seem to return the method to print the display, any suggestions to fix this?

import java.util.Scanner;
public class Lab2
{
String[][] myArray = new String[10][4];

public static void main(String[]args)
{
new Lab2();
}

public Lab2()
{
Scanner input = new Scanner(System.in);

while(true)
{
System.out.println("Welcome to the Contact Directory, please select from following: ");
System.out.println("Type 1 to add contact, 2 to remove contact, 3 to display contacts and 0 to exit.");
int intChoice = input.nextInt();
input.nextLine();

String strFirstName;
String strLastName;
switch(intChoice)
{
case 1:
System.out.println("Enter First Name");
strFirstName = input.nextLine();

System.out.println("Enter Last Name");
strLastName = input.nextLine();

System.out.println("Enter Phone Number");
String strPhoneNumber = input.nextLine();

System.out.println("Enter Age");
String strAge = input.nextLine();

addItem(strFirstName, strLastName, strPhoneNumber, strAge);
break;
case 2:
System.out.println("Enter First Name to Remove");
strFirstName = input.nextLine();

System.out.println("Enter Last Name to Remove");
strLastName = input.nextLine();

removeItem(strFirstName, strLastName);
break;
case3:// GETTING AN ERROR UNREACHABLE STATEMENT
displayItems();
break;
case 0:
System.exit(0);
}
}
}

public void addItem(String strFirstName, String strLastName,
String strPhoneNumber, String strAge)
{

for(int i = 0; i < myArray.length; i++)
{
for(int j = 0; j < myArray[i].length; j++)
{
if (myArray[i][0] == null)
{
myArray[i][0] = strFirstName;
myArray[i][1] = strLastName;
myArray[i][2] = strPhoneNumber;
myArray[i][3] = strAge;
; break;
}
}
}
}

public void removeItem(String strFirstName, String strLastName)
{
int i, j;

for(i = 0; i < myArray.length; i++)
{
for(j = 0; j < myArray.length - 1; j++){
myArray[i][j] = myArray[i + 1][j];
break;
}
}
}

public void displayItems()
{
for(int i = 0; i < myArray.length; i++)
{
for (int j = 0; j < myArray[i].length; j++)
{
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
}


}

Answer

The hint you have should be the code used to fix your problem. Find the next available and add it.

    if (myArray[i][0] == null)
    {
        myArray[i][0] = strFirstName;
        myArray[i][1] = strLastName;
        myArray[i][2] = strPhoneNumber;
        myArray[i][3] = strAge;
        blnInserted = true; 
        break;
    }

You don't need the second loop because you're just checking if the entire entry is null based on the first name. The code should be added in the section where you're confused after removing the enclosing second loop.

Also, the null check after the || is not needed. If it's null you won't be able to check equality since doing so would require an instance which can't be null.