I'm tasked with a project to create a program of a multi-dimensional array that holds First name, Last name, phone number, and age. I'm currently getting the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: input cannot be resolved
at Lab2.<init>(Lab2.java:19)
at Lab2.main(Lab2.java:7)
**Editx2, I think I figured it out. I'd ideally like to remove contacts by first and last name, but I can't figure it out. I tried added a new sysout prompting for last name like I did for first name and then adding another if statement, but it ended up doing something funky instead.
import java.util.Scanner;
public class Lab2 {
static String[][] contacts = new String[10][4];
public static void main (String [] args) {
new Lab2();
}
public Lab2() {
String[][] contacts = new String[10][4];
Scanner input = new Scanner(System.in);
System.out.println("Welcome to my Contacts Directory. Enter a selection below: ");
while(true) {
System.out.println("1: Add a contact");
System.out.println("2: Remove a contact");
System.out.println("3: Display your contacts");
System.out.println("0: Exit the Contacts Directory");
int userChoice = input.nextInt();
switch(userChoice) {
case 1:
addContacts(contacts);
break;
case 2:
removeContacts(contacts);
break;
case 3:
displayContacts(contacts);
break;
case 0:
System.out.println("You are leaving the directory! Goodbye!");
System.exit(0);
}
}
}
private static void addContacts(String[][] contacts) {
Scanner input = new Scanner(System.in);
System.out.println("Enter First Name");
String fName = input.nextLine();
System.out.println("Enter Last Name");
String lName = input.nextLine();
System.out.println("Enter Phone Number");
String num = input.nextLine();
System.out.println("Enter Age");
String age = input.nextLine();
for (int i = 0; i < 10; i++) {
if (contacts[i][0] == null || contacts[i][0].equals(null)) {
contacts[i][0] = fName;
contacts[i][1] = lName;
contacts[i][2] = num;
contacts[i][3] = age;
boolean Inserted = true;
break;
}
}
}
private static void removeContacts(String[][] contacts) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name of the contact you want to remove: ");
String removeContact = input.nextLine();
if (removeContact != null) {
for (int i = 0; i < contacts.length; i++) {
for (int j = 0; j < contacts[i].length; j++) {
if (removeContact.equals(contacts[i][j])) {
contacts[i][0] = null;
contacts[i][1] = null;
contacts[i][2] = null;
contacts[i][3] = null;
break;
}
}
}
}
}
private static void displayContacts(String[][] contacts) {
for(int i = 0; i < contacts.length; i++) {
for(int j = 0; j < contacts[i].length; j++) {
System.out.println(contacts[i][j] + " ");
}
System.out.println();
}
}
}