-2

I keep getting an error when trying to delete or display the users that I've created. Pointing to the line of code where it actually does the deletion or displaying of a certain user. I can't seem to figure out where the error is coming from.

import java.util.*;
public class contactInfo {
    public static void main(String[] args) {
        String tracker[][] = {
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",},
                {" ", " ", " ", " ",}
        };

        Scanner input = new Scanner(System.in);
        int picker = 0;

        while (true) {
            System.out.print("Please choose an option: \n 1. Add a user  \n 2. Delete a user  \n 3. Display a user  \n 4. Quit ");
            picker = input.nextInt();
            if (picker == 1) {
                addUser(tracker);
            } else if (picker == 2) {
                deleteUser(tracker);
            } else if (picker == 3) {
                displayUser(tracker);
            } else {
                break;
            }
        }
    }

    public static String[][] addUser(String[][] add) {
        Scanner input = new Scanner(System.in);

        System.out.print("Which user is this information for (1 - 10): ");
        int user = input.nextInt();
        System.out.println(" ");

        System.out.print("Enter the users first name: ");
        add[user][0] = input.next();
        System.out.println(" ");

        System.out.print("Enter the users last name: ");
        add[user][1] = input.next();
        System.out.println(" ");

        System.out.print("Enter the users phone number (without dashes): ");
        add[user][2] = input.next();
        System.out.println(" ");

        System.out.print("Enter the users age: ");
        add[user][3] = input.next();
        System.out.println(" ");

        return add;
    }

    public static String[][] deleteUser(String[][] del) {
        Scanner input = new Scanner(System.in);

        System.out.print("Which user would you like to delete?: ");
        int user = input.nextInt();

        for (int i = 0; i < del.length - 1; i++) {
            del[user][i] = " ";
        }

        return del;
    }

    public static String[][] displayUser(String[][] displ) {
        Scanner input = new Scanner(System.in);

        System.out.print("Which user would you like to display?: ");
        int user = input.nextInt();

        for (int i = 0; i < displ.length; i++) {
            System.out.print(displ[user][i] + " ");
        }

        return displ;
    }
}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Neil Ruggiero
  • 330
  • 3
  • 4
  • 11
  • your "user" variable is being inputted by your user, you are asking for a value 1-10 and using it as index for your array. Arrays are zero based indexed in Java. So either subtract one from the user input or request 0-9. Also, you should be checking that the index you receive is < displ.length. Although honestly I don't understand why you are not dynamically growing and shrinking your array. Look into ArrayList class in Java and examples of it. – RAEC Jun 15 '16 at 16:41
  • by the way, in your delete and display for loops, instead of using displ.length, use displ[user].length because that is what you are accessing inside the loop!!! that's your exception. but look at my previous comment because you have other mistakes. – RAEC Jun 15 '16 at 16:43
  • Thanks for the info! Haven't covered the ArrayList class in my course yet, but I'll do some research myself. – Neil Ruggiero Jun 15 '16 at 18:44

2 Answers2

0

Your tracker is a 2-dimensional array. 10 rows of 4 strings each. The problem is this code:

    for (int i = 0; i < del.length - 1; i++)
    {
        del[user][i] = " ";
    }

You iterate over the array of 4 strings, but the variable i will go from 0 to 8. What you want is something like:

    for (int i = 0; i < del[user].length; i++)
    {
        del[user][i] = " ";
    }
robinkunde
  • 1,005
  • 9
  • 12
0

Instead of using displ.length or del.length for you limits, use displ[user].length or del[user].length.

Jared
  • 26
  • 4