为什么我的子字符串没有被添加到我的队列中?

时间:2014-10-02 05:20:23

标签: java queue

我正在处理的程序应该让你输入一个性别和一个人的名字ex:“m john” 然后它应该分开男性和女性,并分别打印出名称。

我正在比较字符串中的第一个字符,然后使用enqueue将子字符串添加到男性队列或女性队列,然后我尝试通过打印出列的子字符串来打印每个队列。但是我得到一个错误,即我在for循环中添加了字符串,但我的队列是空的。

public class GenderSorter 
{

    public static void main(String[] args) 
    {
        int numElements;
        int maleCount = 0;
        int femaleCount = 0;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("How many people are you adding: ");
        numElements = keyboard.nextInt();
        keyboard.nextLine();

        ArrayBndQueue male = new ArrayBndQueue<>();
        ArrayBndQueue female = new ArrayBndQueue<>();

        for(int index = 1; index <= numElements; index++)
        {
            /*
            System.out.println("Enter a gender and name (ex: f jenny)");
            String name = keyboard.nextLine();
            System.out.println(name);
            */
            System.out.println("Enter a gender and name (ex: f jenny)");
            String name = keyboard.nextLine();
            char character = name.charAt(0);
            if(character == 'f')
            {
                female.enqueue(name.substring(2));
                femaleCount++;
            }
            else
            {
                male.enqueue(name.substring(2));
                maleCount++;
            }
        }   

            System.out.println("Females: " + "\n");
            for(int index2 = 0; index2 <= femaleCount; index2++)
            {
                System.out.print(female.dequeue());
            }

            System.out.println("Males: " + "\n");
            for(int index3 = 0; index3 <= maleCount; index3++)
            {
                System.out.print(male.dequeue());
            }
    }  
}

这是我的ArrayBndQueue:

public class ArrayBndQueue<T> implements BoundedQueueInterface<T> 
{
    protected final int DEFCAP = 100;
    protected T[] queue;
    protected int numElements = 0;
    protected int front = 0;
    protected int rear;

    public ArrayBndQueue()
    {
        queue = (T[]) new Object[DEFCAP];
        rear = DEFCAP -1;
    }

    public ArrayBndQueue(int maxSize)
    {
        queue = (T[]) new Object[maxSize];
        rear = maxSize -1;
    }

    public void enqueue(T element)
    {
        if(isFull())
        {
            throw new QueueOverflowException("Enqueue " + "attempted on full queue");
        }
        else
        {
            rear = (rear + 1) % queue.length;
            queue[rear] = element;
            numElements++;
        }
    }

    public boolean isFull()
    {
        return (numElements == queue.length);
    }

    public boolean isEmpty()
    {
        return (numElements == 0);
    }

    public T dequeue()
    {
        if(isEmpty())
        {
            throw new QueueUnderflowException("Dequeue" + 
                    " attempted on empty queue!");
        }
        else
        {
            T toReturn = queue[front];
            queue[front] = null;
            front = (front + 1) % queue.length;
            numElements--;
            return toReturn;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

也许这不是唯一的问题,但

for(int index2 = 0; index2 <= femaleCount; index2++)

应该是

for(int index2 = 0; index2 < femaleCount; index2++)

实际上,最后一次出列会给你QueueUnderflowException,因为你试图从只包含n的队列中取出n + 1项。

男性和女性循环都存在同样的问题。