简体   繁体   English

二维联系人数组(Java)

[英]Two-Dimensional Array of contacts (Java)

I'm trying to make a basic program that allows the use to input and remove up to 10 contacts (a [10][4] array supposed to be filled with First Name, Last Name, Phone #, and age). 我正在尝试制作一个基本程序,该程序允许使用它来输入和删除最多10个联系人([10] [4]数组,应该用名字,姓氏,电话号码和年龄填充)。 However when I try to input the 5th contact eclipse gives me an error message. 但是,当我尝试输入第五次月食时,会显示一条错误消息。 I'd like to know why I'm receiving an error message. 我想知道为什么会收到错误消息。 (I assume it's something to do with the columns since it's whenever i'm inputting something >4, but i'm not sure what exactly.) (我认为这与列有关,因为每当我输入> 4时,但我不确定确切是什么。)

import java.util.Scanner;


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

        // This will act as our program switchboard
        public Lab2 (){
            Scanner input = new Scanner(System.in);
            String[][] personalInfo = new String[10][4];

            System.out.println("Welcome to the contacts database.");
            System.out.println("Please select a number from the options below");
            System.out.println("");

            while(true){
                // Give the user a list of their options
                System.out.println("1: Add a new contact.");
                System.out.println("2: Remove an existing contact.");           
                System.out.println("0: Exit the database.");

                // Get the user input
                int userChoice = input.nextInt();

                switch(userChoice){
                    case 1: 
                        addContact(personalInfo);
                        break;
                    case 2: 
                        removeContact(personalInfo);
                        break;              
                    case 0: 
                        System.out.println("Thank you for using the contact database.");
                        System.exit(0);
                }
            }

        }

        private void addContact(String personalInfo[][])
     {

            Scanner input = new Scanner(System.in);
            System.out.println();
            System.out.println( " Hello user, please enter your contact info. (I.E. First and Last Names, Phone #, and Age. Max 10 Contacts)");
            String addedContact = input.nextLine(); 


     int j;
        for(int i = 0; i < personalInfo.length; i++)
        {
            for(j = 0; j < personalInfo.length; j++){
           if(personalInfo[i][j] == null)
           {
               personalInfo[i][j] = addedContact; 
           break;
           }


       }
        }
    }



        private void removeContact(String personalInfo[][]) {
    Scanner input = new Scanner(System.in);
    System.out.print( " Please enter an existing contact that you would like to remove. ");
    String deleteContact = input.nextLine();
    int i , j;

    for(i = 0; i < personalInfo.length; i++)
    {
        for(j = 0; j < personalInfo.length; j++){
    if(personalInfo[i][j].equals(deleteContact))
    {
        personalInfo[i][j] = null;
    break;
    }


        }
    }

        }   
    }

for(j = 0; j < personalInfo[i].length; j++){

You're working with jagged array (or array of arrays) 您正在使用锯齿状数组 (或数组数组)

 String[][] personalInfo = new String[10][4];

so in order to itterate it you should use different lengths for dimensions (in general case the required length is personalInfo[i].length ): 因此,要声明它,您应该为尺寸使用不同的长度(通常情况下,所需的长度为personalInfo[i].length ):

for (int i = 0; i < personalInfo.length; ++i) // <- outer array
  for (int j = 0; j < personalInfo[i].length; ++j) // <- inner length depends on i
    if (personalInfo[i][j] == null) {
      personalInfo[i][j] = addedContact; 
      break;
    }

First things first: 首先要注意的是:

String[][] personalInfo = new String[10][4];

This statement creates a 2dimensional array of Strings, thus creating an array that is capable of holding an array that is capable of holding strings. 该语句创建一个二维字符串数组,从而创建一个能够容纳一个能够容纳字符串的数组。 (in this case 10 arrays that keep 4 strings each) (在这种情况下,10个数组各保留4个字符串)

Now the error: 现在出现错误:

String addedContact = input.nextLine(); 

This line puts the entire line you added in a single string. 该行将您添加的整个行放在单个字符串中。

Now the loops: 1. The outer loop loops correctly through the 10 arrays of strings. 现在开始循环:1.外循环在10个字符串数组中正确循环。 2. The inner loop does not loop through the '2nd dimension', that is the actual array which will hold your contact. 2.内部循环不会循环通过“第二维”,即保持您联系的实际数组。 This loop should be as @Kannan Thangadurai described: 此循环应如@Kannan Thangadurai所述:

    for(j = 0; j < personalInfo[i].length; j++){
      (add relevant code here)
    }

The last error you made is more of a bug, and is in the way you read out the user input. 您犯的最后一个错误更多是一个错误,并且是您读出用户输入的方式。 You actually only ever put a string in the first array and so your four contacts that you saved will all be under personalinfo[1] then you try to add another contact in the fifth place of the array that does not exist, thus raising your error. 实际上,您实际上只在第一个数组中放入了一个字符串,因此您保存的四个联系人都将位于personalinfo [1]下,然后尝试在数组的第五个位置添加另一个不存在的联系人,从而引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM