程序没有调用正确的方法,一直循环回第一个菜单.(Java)



好的,我有一个作为联系人列表的小程序。它是由一个二维数组构造的,它需要用户输入来填充总共10个联系人;因此,该数组有10行4列,由名、姓、电话号码和年龄组成。有一个菜单,用户可以选择1)添加联系人,2)删除联系人,3)显示联系人,以及4)退出程序。我知道这可能看起来像一个粗糙的,蛮力的方法,但它要求我们不要使用任何东西,但我们已经学过的东西,其中不包括方法,如ArrayList, LinkedList, Collections, HashMaps等。我的问题是,程序工作正常,直到我删除一个联系人。当我再次得到菜单时,我想重新添加联系人,它就会循环回到菜单。这一定是一些小语法错误,但我不确定在哪里。我将展示代码和输出,以便让您了解它从哪里开始出错。*注:我并不是要求代码本身,而是要求建议和指导,让我意识到我的错误所在。

代码:

    import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
    new ContactList ();
}
    public ContactList () {
        Scanner input = new Scanner(System.in);
        String[][] contactList = new String[10][4];

        System.out.println("Welcome to your Contact organizer.");
        System.out.println("You can store " + contactList.length + " contacts. " );
        System.out.println("Please enter your choice from the list below: ");
        System.out.println();
        while(true){
            System.out.println("1: Add a Contact"); //the user options
            System.out.println("2: Remove a Contact");
            System.out.println("3: Display your Contacts");
            System.out.println("4: Exit this program");
            int userSelection = input.nextInt();
            switch(userSelection) {
                case 1:
                    addContact(contactList);
                    break;
                case 2:
                    removeContact(contactList);
                    break;
                case 3:
                    displayContacts(contactList);
                    break;
                case 4:
                    System.out.println("Goodbye.");
                    System.exit(0);
            }
        }
    }
private void addContact(String contactList[][]) {
    Scanner input = new Scanner(System.in);
    for(int i = 0; i < contactList.length; i++) {
        if(contactList[i][0] == null) {
            System.out.print("Enter the contact's first name: ");
            contactList[i][0] = input.nextLine().trim(); //.trim() eliminates any accidental white spaces
            System.out.print("Enter the contact's last name: ");
            contactList[i][1] = input.nextLine().trim();
            System.out.print("Enter the contact's phone number: ");
            contactList[i][2] = input.nextLine().trim();
            System.out.print("Enter the contact's age: ");
            contactList[i][3] = input.nextLine().trim();
            break;
        }
    }
}//end add method
private void removeContact(String contactList[][]){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the first name of contact you wish to remove: ");
    String deleteFirst = input.nextLine().trim();
    System.out.println("Enter the last name of contact you wish to remove: ");
    String deleteLast = input.nextLine().trim();
    for(int i = 0; i < contactList.length; i++) {
        for(int j = 0; j < contactList[i].length; j++) {
            if(deleteFirst.equals(contactList[i][0]) && deleteLast.equals(contactList[i][1])) {
                System.out.println("Contact successfully removed. n n");
                contactList[i][0] = null;
                contactList[i][1] = null;
                contactList[i][2] = null;
                contactList[i][3] = null;
                break;
            }
        }
    }
}//end remove method
private void displayContacts(String contactList[][]) {
    for(int i = 0; i < contactList.length; i++) {
        for(int j = 0; j < contactList[i].length; j++) {
            if(contactList[i][j] == null) {
                contactList[i][j] = (" "); //if user displays before all contacts are entered, it will display blank rather than null null null, etc.
            }
            System.out.print(contactList[i][j] + " ");
        }
        System.out.println();
    }
}//end display method
}//end class

我知道removeccontact方法可能看起来很可怕,但这是我知道如何删除整行的唯一方法。我一直在想我应该在那个方法中创建一个新数组,但我还不知道怎么做。

输出如下:

    Welcome to your Contact organizer.
    You can store 10 contacts. 
    Please enter your choice from the list below: 
    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    1
    Enter the contact's first name: John
    Enter the contact's last name: Doe
    Enter the contact's phone number: 1234567
    Enter the contact's age: 34
    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    1
    Enter the contact's first name: Joe
    Enter the contact's last name: Bob
    Enter the contact's phone number: 4567890
    Enter the contact's age: 30
    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    3
    John Doe 1234567 34 
    Joe Bob 4567890 30  --contacts display just fine so far

    --I know there are a lot of spaces since technically there is 
      still data here, albeit blank spaces. Perhaps I need to make it a 
      new array each time something is added? I'll have to figure out
      how to do that. For now, it looked far better than: 
      null null null null
      null null null null 
      etc...


    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    2
    Enter the first name of contact you wish to remove: 
    Joe
    Enter the last name of contact you wish to remove: 
    Bob
    Contact successfully removed. 

    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    3
    John Doe 1234567 34 --output displays contact removed




    1: Add a Contact         --and this is where is begins to mess up. 
    2: Remove a Contact        Why now? 
    3: Display your Contacts
    4: Exit this program
    1
    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    1
    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    1
    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    3
    John Doe 1234567 34       --I can still display contacts. ????




    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    1
    1: Add a Contact
    2: Remove a Contact
    3: Display your Contacts
    4: Exit this program
    4
    Goodbye.                 --and I can exit. I just can no longer add.
    Process finished with exit code 0

我发现了一个问题:

    if(contactList[i][j] == null) {
            contactList[i][j] = (" "); //if user displays before all contacts are entered, it will display blank rather than null null null, etc.
        }

我将其更改为只在联系人不为空时显示联系人,如下所示:

    if(contactList[i][j] != null) {
                System.out.print(contactList[i][j] + " ");
            }

我现在可以返回到我的菜单并添加另一个联系人。我更希望输出没有留下空白,但我会自己弄清楚,因为它与我的原始帖子无关。

虽然我不知道为什么这个问题会发生(因为我没有收到任何答案或评论,只有一个毫无意义的反对投票),我至少解决了我的问题。我唯一的猜测是,当我将联系人赋值为null,然后将该空值赋给空白(" ")时,程序认为它在技术上仍然是满的,因为数组现在被空格的unicode值填充("u0020")。因此,我对输出的美学尝试在程序中创造了一个中断。

相关内容

  • 没有找到相关文章

最新更新