-2
import java.util.Stack;
Stack stack = new Stack();

void setup(){
    stack.push('1');
    println(stack);
}

IDE says: "the function push(String) does not exist"

lisichka ggg
  • 563
  • 3
  • 15

2 Answers2

1

Your code works fine.

import java.util.Stack;
Stack stack = new Stack();
stack.push("1");
println(stack);

Notice that I've changed the '1' to a "1", which I'm guessing is what you're actually doing.

This correctly prints out [1] for me.

You might want to add generics:

import java.util.Stack;
Stack<String> stack = new Stack<String>();
stack.push("1");
println(stack);

This will guarantee that you only add String values to your Stack. But it should work fine without this as well.

My guess is you just forgot the stack. part:

push("1");

This will generate the error you mentioned. Fix this by adding stack. in front of the function.

In the future, please make sure that the code you post in your question is actually the code you're running.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

String in java are double quotes "string" instead of 'string'.
Moreover I found that for me, following code is working [confirmation image here]

    Stack stack = new Stack();
    stack.push('1');
    System.out.println(stack.pop());

So,probably reason of this could be generics.
As you know java.util.Stack is generic framework.
So will suggest you to use Stack<String> stack = new Stack<>();

Randhawa
  • 226
  • 1
  • 15