2017-02-16 65 views
-1

我的任务是创建一个包含名字,姓氏,电话号码和年龄的多维数组的程序。我目前得到的错误:添加,删除和显示方法

异常在线程“主要” java.lang.Error的:未解决的问题,编译: 输入无法解决

at Lab2.<init>(Lab2.java:19) 
at Lab2.main(Lab2.java:7) 

** Editx2,我想我想通了。我最好喜欢用名字和姓氏来删除联系人,但我无法弄清楚。我尝试添加一个新的sysout来提示姓,就像我为名字所做的那样,然后添加另一个if语句,但最终却做了一些简单的事情。

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(); 
     } 
    } 
} 
+0

这里有类似的答案,但我无法从他们的代码中找出它。它非常相似。 – JWags

+0

removeContacts方法不起作用 – JWags

+0

构造函数的作用是初始化实例变量,你的代码中有很多不必要的东西。我建议你将while循环移出它。 –

回答

-1

您的“输入”变量[object]不是全局变量,它是另一个函数中的作用域变量。