# Can someone do me a favor and help me to find the error in this code

``````public class Queue {
//concept of this code is first in first out/last in last out, delete(dequeue)  the item at the front (start at location =0) and add (dequeue)at (location rear)
private int front;
private int rear;
private int count;
int[]A;
public Queue(int size){
front=rear=count=0;
A=new int [size];
}
public void dequeue(){
//this method should delete at location 0 (front) and increment by 1 if  front is equal to size of the array, front =0
if(front<=count-1)
{A[front]=0;
front++;
}
if(front==count)
front=0;

}

public void enqueue(int element)
{
int count2=0;
int count3=rear+1;
if(rear<A.length-1)
{
A[count]=element;
rear=count;
count+=1;

}
if(front ==0 && rear==count-1)
{//if the frony =0 (there is no empty location to add a new element) and rear equals to size of the array -1 (last index),create new array of size count*2+1 and move the element to the new array (incease size)
int temp[]=new int[count*2+1];
for(int i=front;i<=rear;i++)
temp[i]=A[i];
A=temp;
}
if(rear==count-1 && front>0 && front>count2)
{
A[count2]=element;
rear=count2;
count2++;

}
if (rear<front && rear+1!=front&& count3<front){
A[count3]=element;
rear=count3;
count3++;

}
if(rear<front&& rear+1==front)
{
int temp[]=new int[count*2+1];
for (int i=front;i<=count-1;i++)
temp[i]=A[i];
for(int j=0;j<=rear;j++)
temp[j]=A[j];
A=temp;

}

}

public void print(){
for(int i =0;i<=count-1;i++){
System.out.println(A[i]);
}
}
public static void main(String[] args) {

Queue q=new Queue (5);
q.enqueue(2);
q.enqueue(2);
q.enqueue(2);
q.enqueue(2);
q.enqueue(2);
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
q.enqueue(5);
q.enqueue(6);
q.enqueue(7);
q.print();

}

}

``````

That is not how it works here on the forum, we can help you, but we won’t just go and fix code. You will have to do that (with our guidance of course )

What is not working as expected? Please be a little bit more descriptive then just dumping some code with no comments or description of how it works

it is the first time iIuse this website, thus I don’t know what I should do.
thank you for informing me that, I will write some comments that will make things clear.

1 Like

there is a new user section:

maybe you can find some useful information there, or in the guidelines

We help people (even with questions unrelated to exercises on codecademy), but we don’t write fixes, that is outside the scope of help.

1 Like

Is your question exercise related? If its not, it should be in the corner bar. I moved it there for a reason

the java data structure section on the forum is reserved for question related to syllabus 4 data structures of the java course:

also, here:

``````public class Queue {
``````

there is no matching `}` for the `{` of you Queue class

and where is the main method:

https://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

i can’t find it

No, it is not.

I added the main method to the code.

i put the code here:

https://repl.it/@stetim94/OpenOvercookedAsiantrumpetfish

i added the main method to a main class, given otherwise i can’t run it on repl, and its a better design. What is your question?

enqueue add elements to the array. when there is no place for more element it creates a new array and moves the element to it. if there is a place in the array, the element add at rear
+1 and increment rear by 1. enqueue create a new array even though there is a location in the array to add a new element.
thank you for helping me, I am learning java but I always face problems when I implement a method I wrote it in a paper first .

you should really import Arrays:

``````import java.util.Arrays;
``````

given it contains some very useful methods.

things like this:

``````if(front<=count-1)
``````

are just stupid, there is no other word for it. You should really use `if( front < count)`, its so much easier.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.