0

I have one array (25*ax+c-d/2) and i want to put everything in a stack and next remove the operators ( *;+;-;/) and have at the end (25;ax;c;d) in a stack implementation.

Right now i have :

import java.util.*; 
  
public class StackDemo { 
    public static void main(String args[]) 
    { 
      	//Creating array
      	ArrayList<String> al = new ArrayList<String>();
      
      	//Adding to array
      	al.add("25*ax+c-d/2");
      
      	
      	//Creating a stack 
        Stack<String> STACK = new Stack<String>(); 
  
        //Adding array to stack 
        for(String str : al)
  			STACK.add(str);
  
        // Displaying the Stack 
        System.out.println("Initial Stack: " + STACK); 
  
    } 
} 

I know I'm grouping everything in the same stack position, but how can I separate and then remove the operators?

3
  • 2
    Your solution is pretty far away from being functional. As a hint, you can start by writing some logic which can identify one or more tokens which might appear in your algebraic expression. Here, a token means for example a number (e.g. 25) or an operator (e.g. + or -). Then, your code needs to read each token, one at a time, and figure out what to do with them, by putting them on the stack, or doing a math operation. Commented Oct 22, 2019 at 9:54
  • i dont need to a math operation , i just need to remove the operators of the array and put the result on different positions of the stack. Commented Oct 22, 2019 at 10:00
  • Why? Hard to see the utility of this. Surely at some point you are going to evaluate the expression? Don't design the solution before you fully understand the problem. Hint: Dijkstra Shunting-yard algorithm. Commented Oct 22, 2019 at 10:04

2 Answers 2

1

You can use regex to split the expression with operators:

public static void main(String[] args) {
    //Creating array
    List<String> al = Arrays.asList("25*ax+c-d/2".split("[\\*+/-]"));

    //Creating a stack 
    Stack<String> STACK = new Stack<String>(); 

    //Adding array to stack 
    for(String str : al)
        STACK.add(str);

    // Displaying the Stack 
    System.out.println("Initial Stack: " + STACK); 
}

Output:

Initial Stack: [25, ax, c, d, 2]
Sign up to request clarification or add additional context in comments.

2 Comments

The only thing you need to escape here is *.
You're right, it should be "[\*+/-]", i'm updating the answer.
0

From JavaDoc reference of Stack class:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.

Deque<String> stack = new ArrayDeque<String>();

In your scenario, it is possible to initialize a Deque by passing an ArrayList directly:

List<String> al = Arrays.asList("25*ax+c-d/2".split("[\\*+/-]"));
Deque<String> stack = new ArrayDeque<String>(al);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.