Question: hey guys. so i have the following program in java ( eclipse ) for impleminting stack i did everything as it asked but i got

hey guys. so i have the following program in java ( eclipse ) for impleminting stack

i did everything as it asked but i got stucked in the last part .. please help me out as soon as possible

// 1- how to traverse through the user input and push charachters into the stack??

// 2- write a loop that pops a character from the stack and prints that character until the stack is empty.

// 3-If your stack is correct, then the string the user enters will be printed backwards

please the answer should be well orieted and clear becasue i need to understand!

thank you in advance!

here is the code:

import java.util.EmptyStackException;

import java.util.Scanner;

public class ArrayStack implements Stack{ //implementing the stack

private E[] a;

private int size;

private String convert;

public ArrayStack() {

a = (E[]) new Object[10];

size = -1;

}

public void push(E e) { //to add to the stack

if (size == a.length) {

throw new IllegalStateException();

}

else {

a[size] = e;

size++;

}

}

public E pop() { //delete and returns the value

if (size == 0) {

throw new EmptyStackException();

}

E top = a[size-1];

size--;

return top;

}

public E peek() { //check the value of the stack

if (size == 0) {

throw new EmptyStackException();

}

return a[size -1];

}

public boolean empty() { //to check if the stack is empty.

if(size == 0) {

return true;

}else {

return false;

}

}

public static void main(String[] args) {

//Read a string entered by the user

ArrayStack a = new ArrayStack();

System.out.println("Enter a string:" );

Scanner scanner = new Scanner(System.in);

String tra =scanner.nextLine();

// 1- how to traverse through the user input and push charachters into the stack??

// 2- write a loop that pops a character from the stack and prints that character until the stack is empty.

// 3-If your stack is correct, then the string the user enters will be printed backwards

for (int i = 0 ; i < tra.length(); i++) {

char c = tra.charAt(i);

a.push(c);

System.out.println(c);

}

while(!a.empty()) {

char b = a.pop();

System.out.println(b);

}

// THIS PART IS A SAPERATE METHOD THAT IT ONLY NEEDS TO MATCH THE PARENTHESIS

/**char c;

for( int i = 0 ; i < tra.length(); i++) {

c = tra.charAt(i);

if( c== '[' || c == '{' || c == '(') {

a.push(c);

} else if( c == ']' || c == '}' || c == ')' && !a.empty()) {

if((char)a.peek() == '(' && c == ')' || (char)a.peek() == '{' && c == '}' || (char)a.peek() == '[' && c == ']'){

a.pop();

} else {

System.out.println("not balanced");;

}

} else {

if (( c == ']' || c == '}' || c == ')')) {

System.out.println("not balanced");

}

}

}

if(a.empty()) {

System.out.println("Balanced Parenthisis");

}else {

System.out.println("not balanced");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!