2011-06-22 33 views
-2

此代碼似乎不想編譯。我對Java編程很陌生。任何幫助將非常感激。錯誤表示找不到符號。無法獲得此代碼編譯,任何想法?

package addressbookexample1; 

import java.io.*; 
import java.util.Scanner; 

public class AddressBookExample1 { 

    private Contact[] friends; 
    private int numfriends; 

    // Create an empty AddressBook 
    public AddressBookExample1() { 
     friends = new Contact[10]; 
     numfriends = 0; 
    } 

    // Add a contact that's passed in as a parameter. 
    public void addContact(Contact c) { 
     friends[numfriends] = c; 
     numfriends++; 
    } 

    // Print out info on all contacts using method Contact class. 
    public void printContacts() { 
     for (int i = 0; i < numfriends; i++) { 
      friends[i].printContact(); 
     } 
    } 

    // Returns the number of friends currently in AddressBook 
    public int numContacts() { 
     return numfriends; 
    } 


    private int haveContact(String s) { 

     for (int i = 0; i < numfriends; i++) { 
      if (friends[i].getName().equals(s)) { 
       return i; 
      } 
     } 
     return -1; 
    } 

    // Deletes a contact with name 
    public void deleteContact(String s) { 

     int place = haveContact(s); 
     if (place >= 0) { 
      friends[place] = friends[numfriends - 1]; 
      numfriends--; 
     } 
    } 


    public static void main(String[] args) throws IOException { 

     Scanner stdin = new Scanner(System.in); 

     // Instantiate AddressBook object 
     AddressBook blackbook = new AddressBook(); 

     // Menu driven loop. 
     menu(); 
     int choice = stdin.nextInt(); 

     while (choice != 5) { 


      if (choice == 1) { 

       if (blackbook.numContacts() < 10) { 

        //Reads in all appropriate information."); 
        System.out.println("Enter your friend\'s name:"); 
        String name = stdin.next(); 
        System.out.println("Enter their age."); 
        int age = stdin.nextInt(); 
        System.out.println("Enter their phone number."); 
        int number = stdin.nextInt(); 
        System.out.println("Enter the birthday, month on one line, then day on the next."); 
        int mon = stdin.nextInt(); 
        int day = stdin.nextInt(); 


        blackbook.addContact(new Contact(name, age, number, mon, day)); 
       } else { 
        System.out.println("Sorry, can not add anyone, your blackbook is full."); 
       } 
      } 
      else if (choice == 2) { 
       System.out.println("What is the name of the contact you want to delete?"); 
       String name = stdin.next(); 
       blackbook.deleteContact(name); 
      } else if (choice == 3) { 
       System.out.println("You have " + blackbook.numContacts() + " contacts."); 
      } else if (choice == 4) { 
       blackbook.printContacts(); 
      } else if (choice != 5) { 
       System.out.println("Sorry, that was an invalid menu choice, try again."); 
      } 

      menu(); 
      choice = stdin.nextInt(); 
     } 

    } 

    public static void menu() { 
     System.out.println("1.Add a new contact to your address book."); 
     System.out.println("2.Delete a contact from your address book."); 
     System.out.println("3.Print out the number of contacts you have."); 
     System.out.println("4.Print out information of all of your contacts."); 
     System.out.println("5.Quit."); 
     System.out.println("Enter your menu choice:"); 
    } 
} 

編譯器錯誤 異常在線程 「主」 了java.lang.RuntimeException:不可編譯的源代碼 - 找不到符號 符號:類addressbookexample1.AddressBookExample1 在addressbookexample1.AddressBookExample1.main

: 位置類通訊錄
+0

請複製/粘貼確切的錯誤信息。行號和錯誤代碼對診斷問題非常有幫助。 – BraedenP

+0

什麼是錯誤? – Marcin

+0

你能複製編譯錯誤嗎? –

回答

0

看起來像Contact類缺失。我認爲這也是該示例的一部分,並且您還需要在構建中包含源代碼。

+0

這只是我想一個例子弄清楚。我如何定義這一點。非常感謝! – PittsburghCoder

+0

在示例代碼中的其他地方查找Contact.java。或寫一個新的。 :) –

0

您還沒有定義Contact類

+0

這只是我想弄明白的一個例子。我如何定義這一點。非常感謝! – PittsburghCoder

0

看代碼,它看起來像聯繫可能無法使用。嘗試同時編譯Contact.java和這個類。

2

您將此定義爲AddressBookExample1,但您試圖實例化AddressBook。

// Instantiate AddressBook object 
AddressBook blackbook = new AddressBook(); 

更改爲

AddressBookExample1 blackbook = new AddressBookExample1(); 
0

你試圖使用類通訊錄,但您已經定義AddressBookExample1。

0

如果這是一個示例,應該有其他示例代碼,它定義了「Contact」和「Addressbook」類。您應該確保您完全按照提供的內容輸入示例代碼。也許你已經將AddressBook類的名稱更改爲AddressBookExample1?