Good, I'm doing a job for my university, this consists among other things in inserting caracateres in 3 batteries, 1 of them has a sequence which we want to find in the other 2 batteries.
The problem is that in the method Comparar
that I will leave next I miss an error that also has to do with the method pop
of the stack, I will leave the code and the error that jumps to see if I could help.
(the code is not complete, I only have the comparison of the first stack with the sequence stack).
Class of the stack:
public class Pila {
char elementos[];
int tope;
public Pila(int n) {
elementos= new char[n];
tope = -1;
}
public boolean empty(){
if(tope == -1){
return true;
}else{
return false;
}
}
public boolean full(){
if(elementos.length-1 == tope){
return true;
}else{
return false;
}
}
public void push(char a){
tope++;
elementos[tope]=a;
}
char aux;
public char pop(){
aux= elementos[tope];
tope--;
return aux;
}
public char peek(){
return elementos[tope];
}
public void mostrar(){
for(int i=0;i<elementos.length;i++){
System.out.print(elementos[i]+", ");
}
}
}
/*metodos de comparacion, copia(ya que tengo que comparar con el pop y no quiero que se e elimine la pila original) y el main*/
public class Tarea2 {
//cmetodod para comparar
public static boolean Comparar(Pila x,Pila primera){
// se copian las pilas a comparar
Pila copia1=copia(x); // pila con secuencia
Pila copiaprimera= copia(primera);// pila a comparar con la secuencia
for(int i =0;i<=x.tope;i++){
copia1.elementos[i]=x.elementos[i];
}
for(int i =0;i<=x.tope;i++){
copiaprimera.elementos[i]=primera.elementos[i];
}
int contador=0;
char c= copia1.pop();
char b= copiaprimera.pop();
System.out.println(c);
// a continuacion se comparan los caracteres de la pila
while(contador < 3){
if(c==b){
c=copia1.pop();
b=copiaprimera.pop();
contador++;
}else{
b=copiaprimera.pop();
}
}
if(contador==3){
return true;
}
return false;
}
public static Pila copia(Pila original){
Pila copia = new Pila(original.elementos.length);
for(int i=0; i<copia.elementos.length;i++){
copia.elementos[i]=original.elementos[i];
}
return copia;
}
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
System.out.println("Ingrese una serie de caracteres para la primera pila: ");
String x = teclado.nextLine();
Pila primera = new Pila(x.length());
for( int i=0; i<x.length();i++){
primera.elementos[i]= x.charAt(i);
}
primera.mostrar();
System.out.println();
System.out.println("Ingrese una serie de caracteres para la segunda pila: ");
String y = teclado.nextLine();
Pila segunda = new Pila(y.length());
for( int i=0; i<y.length();i++){
segunda.elementos[i]= y.charAt(i);
}
segunda.mostrar();
System.out.println();
System.out.println("Ingrese una serie de caracteres para la pila secuencia: ");
String z = teclado.nextLine();
Pila secuencia = new Pila(z.length());
for( int i=0; i<z.length();i++){
secuencia.elementos[i]= z.charAt(i);
}
secuencia.mostrar();
System.out.println();
// llama a a la funcion para comparar
System.out.println(Comparar(secuencia,primera));
}
}
This is the error you sent me:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at tarea2.Pila.pop (Pila.java:46) at tarea2.Tarea2.Comparar (Tarea2.java:32) at task2.Tarea2.main (Task2.java:102) C: \ Users \ User \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53: Java returned: 1 BUILD FAILED (total time: 22 seconds)