2017-02-16 5 views
-1

私は、名前、姓、電話番号、および年齢を保持する多次元配列のプログラムを作成するプロジェクトが用意されています。私は現在、エラーを取得しています:スレッドでメソッドの追加、削除、および表示

例外「メイン」でjava.lang.Error:未解決のコンパイルの問題: 入力は

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

を解決することはできません。** Editx2、私はそれを考え出したと思います。私は理想的には名字で連絡先を削除したいのですが、わかりません。私は最初の名前のためにやっていたように、別の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

"入力"変数[オブジェクト]はグローバル変数ではなく、別の関数のスコープ付き変数です。

関連する問題