java.lang.RuntimeException:无法编译的源代码

时间:2014-12-17 07:31:08

标签: java

这是完整的代码,这个程序应该可以工作,因为我从书中复制了它:

Java:初学者指南:Herbert Schildt

Class FixedQueue:

// A fixed-size queue class for characters that uses exceptions.
class FixedQueue implements ICharQ {
    private char q[]; // this array holds the queue
    private int putloc, getloc; // the put and get indices

    // Construct an empty queue given its size.
    public FixedQueue(int size) {
        q = new char[size]; // allocate memory for queue
        putloc = getloc = 0;
    }

    // Put a character into the queue.
    public void put(char ch) throws QueueFullException {

        if(putloc==q.length)
            throw new QueueFullException(q.length);

        q[putloc++] = ch;
    }

    // Get a character from the queue.
    public char get() throws QueueEmptyException {

        if(getloc == putloc)
            throw new QueueEmptyException();

        return q[getloc++];
    }
}

接口ICharQ:

// A character queue interface.
public interface ICharQ {
    // Put a character into the queue.
    void put (char ch) throws QueueFullException;

    // Get a character from the queue.
    char get() throws QueueEmptyException;
}

班级QExcDemo:

// Demonstrate the queue exceptions.
class QExcDemo {
    public static void main(String args[]) {
        FixedQueue q = new FixedQueue(10);
        char ch;
        int i;

        try {
            // over-empty the queue
            for(i=0; i<11; i++) {
                System.out.print("Getting next char: ");
                ch = q.get();
                System.out.println(ch);
            }
        }
        catch(QueueEmptyException exc) {
            System.out.println(exc);
        }
    }
}

Class QueueEmptyException:

// An exception for queue-empty errors.
public class QueueEmptyException extends Exception {

    public String toString() {
        return "\nQueue is empty.";
    }
}

Class QueueFullException:

// An exception for queue-full errors.
public class QueueFullException extends Exception {
    int size;

    QueueFullException(int s) { size = s; }

    public String toString() {
        return "\nQueue is full. Maximum size is " + size;
    }
}

当我构建程序时,为什么有关于异常的错误?

  

run:线程“main”中的异常java.lang.RuntimeException:   无法编译的源代码 - dec15_javatutorial.FixedQueue中的get()   无法在dec15_javatutorial.ICharQ覆盖中实现get()   方法不会抛出dec15_javatutorial.QueueEmptyException获取   next char:at dec15_javatutorial.FixedQueue.get(FixedQueue.java:28)     在dec15_javatutorial.QExcDemo.main(QExcDemo.java:33)Java结果:1

由于

1 个答案:

答案 0 :(得分:3)

我已将所有课程复制到项目中并运行它。它编译并运行。

输出:

Getting next char: 
Queue is empty.

我的猜测是你在FixedQueue中改变了一些内容而忘记了保存或者导入的内容不正确。