1

I want to create a stack of Integer stack. So that I can push a new integer stack in main stack.

How to create it? I have done the following code for that:

import java.util.*;
import java.io.*;

public class Containers{
    static Stack<Integer> subStack = new Stack<Integer>();
    static Stack<subStack> main  = new Stack<subStack>();
   public static int addToStatck(char cont){
       //for(int j=0;j<)
        return 0;
    }
    public static void main(){
        Scanner sc = new Scanner(System.in);
        String container = sc.next();
        Stack sub = new Stack();
        sub.push(container.charAt(0));
        main.push(sub);
        int ans = 0;
        for(int i=0;i<container.length();++i){
            ans+=addToStatck(container.charAt(i));
        }

    }

}

it Shows me the following output: enter image description here

1 Answer 1

3

There is no subStack type (unless you create such type).

The type of main should be:

static Stack<Stack<Integer>> main = new Stack<>();

And there doesn't seem to be any use to your

static Stack<Integer> subStack = new Stack<Integer>();

variable.

Besides,

Stack sub = new Stack();

should be

Stack<Integer> sub = new Stack<>();

To summarize:

public class Containers{
    static Stack<Stack<Integer>> main = new Stack<>();
    public static int addToStatck(char cont){
        //for(int j=0;j<)
        return 0;
    }
    public static void main(){
        Scanner sc = new Scanner(System.in);
        String container = sc.next();
        Stack<Integer> sub = new Stack<>();
        sub.push(Integer.valueOf (container.charAt(0)));
        main.push(sub);
        int ans = 0;
        for(int i=0;i<container.length();++i){
            ans+=addToStatck(container.charAt(i));
        }   
    }  
}
Sign up to request clarification or add additional context in comments.

8 Comments

import java.util.*; import java.io.*; public class Containers{ static Stack<Stack<Character>> main = new Stack(); public static int addToStatck(char cont){ return 0; } public static void main(){ Scanner sc = new Scanner(System.in); String container = sc.next(); Stack<Character> sub = new Stack<Character>(); sub.push(container.charAt(0)); main.push(sub); int ans = 0; for(int i=0;i<container.length();++i){ ans+=addToStatck(container.charAt(i)); } } }
I have done the above code. But it still showing me the error: Container.java contains unchecked or unsafe operation.
@Andy.inamdar Either change your inner Stack to a Stack<Character>, or change sub.push(container.charAt(0)); to sub.push(Integer.valueOf (container.charAt(0)));
@Andy.inamdar you can't add a char to a Stack of Integer, since there's no automatic conversion from char to Integer.
I haven't created the stack of integer. I have created the of character.
|

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.