无法获取此代码编译,任何想法?

时间:2011-06-22 22:22:39

标签: java compiler-errors

此代码似乎不想编译。我是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:");
    }
}

编译器错误 线程“main”中的异常java.lang.RuntimeException:无法编译的源代码 - 找不到符号   符号:类AddressBook   location:class addressbookexample1.AddressBookExample1     at addressbookexample1.AddressBookExample1.main

6 个答案:

答案 0 :(得分:2)

您将其定义为AddressBookExample1,但您正在尝试实例化AddressBook。

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

更改为

AddressBookExample1 blackbook = new AddressBookExample1();

答案 1 :(得分:0)

看起来缺少Contact类。我假设这也是示例的一部分,您还需要在构建中包含它的源代码。

答案 2 :(得分:0)

您尚未定义Contact类

答案 3 :(得分:0)

查看代码,看起来可能无法使用联系人。尝试同时编译Contact.java和这个类。

答案 4 :(得分:0)

您正在尝试使用AddressBook类,但您已经定义了AddressBookExample1。

答案 5 :(得分:0)

如果这是一个示例,则应该有样本所在的其他代码,它定义“联系人”和“地址簿”类。您应确保完全按照提供的方式输入示例代码。也许你已经将AddressBook类的名称更改为AddressBookExample1?