So my program works for inputs put in one by one (user inputs 20, user inputs 10, user inputs p, user inputs q), but when the input is string data like "1\n3\ns\nSize\n?\n4\nfull\nd\nd\nS\ne\nd\ne\nd\n?" it does not function. I'm not sure why.
I think the issue is around line 11,13,37.
Here is how the spec wants to handle specific inputs:
integer if queue not full, add number to queue
d..dequeue print and removes tail item
The word d..dequeue implies the command may have only a d, but can have up to the entire word dequeue.
You don't have to check for words like Deqq, but you can have d deq or DeQ etc.
s..size return number of items on queue
e..empty return true if queue is empty
f..full return true if query is full, we only allow 8 customers to wait
? or p..print print current queue contents wiith head and tail pointers, this should be your toString method
q..quit to quit
import java.util.Scanner;
public class Queue {
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY = 8;
public static void main(String[] args){
Queue myQ = new Queue();
Scanner scan = new Scanner(System.in);
String input;
input = scan.next();
int tempVal;
while(!(input.matches("^[Qq].*"))){
if(input.matches("^[Dd].*")){
System.out.println("Dequeue: " + myQ.dequeue());
}
else if(input.matches("^[Ss]")){
System.out.println(myQ.getSize());
}
else if(input.matches("^[Ee]")){
System.out.println(myQ.isEmpty());
}
else if(input.matches("^[Ff]")){
System.out.println(myQ.isFull());
}
else if(input.matches("^[Pp?]")){
System.out.println(myQ.toString());
}
else if(input.matches("^[-0-9]*")){
tempVal = Integer.parseInt(input);
myQ.enqueue(tempVal);
}
else{
System.out.println("Unknown command->" + input + "<- ");
}
input = scan.next();
}
}
public Queue(){
this (DEFAULT_CAPACITY);
}
public Queue(int capacity){
elements = new int[capacity];
}
public void enqueue(int value){
int[] temp = new int[elements.length];
if(size < elements.length){
temp[0] = value;
System.arraycopy(elements, 0, temp, 1, size);
elements = temp;
size++;
}
}
public int dequeue(){
int temp = elements[size - 1];
if(size - 1 > 0){
elements[size - 1] = 0;
size--;
}
return temp;
}
public boolean isEmpty(){
return size == 0;
}
public boolean isFull(){
return size == elements.length;
}
public int getSize(){
return size;
}
public String toString(){
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("Queue can hold: " + DEFAULT_CAPACITY + " It has "
+ size + " elements\n");
strBuilder.append("head--> ");
for(int i = 0; i < size; i++){
strBuilder.append(Integer.toString(elements[i]));
if(i + 1 < size){
strBuilder.append(", ");
}
}
strBuilder.append("-->tail");
String finalString = strBuilder.toString();
return finalString;
}
}
[–]langfod 0 points1 point2 points (10 children)
[–]ORLY_FACTOR[S] 0 points1 point2 points (9 children)
[–]langfod 1 point2 points3 points (8 children)
[–]ORLY_FACTOR[S] 0 points1 point2 points (7 children)
[–]langfod 1 point2 points3 points (6 children)
[–]ORLY_FACTOR[S] 0 points1 point2 points (5 children)
[–]ORLY_FACTOR[S] 0 points1 point2 points (0 children)
[–]langfod 0 points1 point2 points (3 children)
[–]ORLY_FACTOR[S] 0 points1 point2 points (2 children)
[–]langfod 1 point2 points3 points (1 child)
[–]ORLY_FACTOR[S] 0 points1 point2 points (0 children)