2014-10-02 41 views
0

我正在工作的程序应该输入一个人的性别和姓名,例如:“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; 
     } 
    } 
} 

回答

2

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

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

应该

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

因为您试图从仅包含n的队列中取出n + 1个项目,所以最后的出列会给您QueueUnderflowException

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

相关问题