Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show nothing on the console via System.out.println?
    primarykey
    data
    text
    <p>Here is a question about Stack on StackOverflow. </p> <p>My question might seem very very vague but if you check my program which I have written then you might understand what I am trying to ask. I have implemented the stack myself. I present the user with 3 choices. Push, Pop and View the stack. When view(display) method is called then bunch of 0s show instead of nothing. We know the stack contains nothing unless we put something on it. But since my implemented stack is stack of integers using array, the display method when called shows bunch of 0s(the default values of integers in the array). How do I show nothing instead of 0s. I know I can add ASCII for whitespace character but I think it would still violate the rule of stack(Stack should be empty when there is not element, not even code for whitespace). </p> <p>Here is my program:</p> <pre><code>import java.util.Scanner; public class StackClass { public static void main(String []args) { Scanner input=new Scanner(System.in); int choice=0; int push; Stack stack=new Stack(); do { System.out.println("Please select a stack operation:\n1. Press 1 for adding to stack\n2. Press 2 for removing elements from stack\n3. View the stack"); choice=input.nextInt(); switch(choice) { case 1: System.out.println("Please enter the number that you want to store to stack"); push=input.nextInt(); stack.push(push); case 2: stack.pop(); case 3: stack.display(); } } while((choice==1)||(choice==2)||(choice==3)); } } class Stack { private int size; private int[] stackPlaces=new int[15]; private int stackIndex; Stack() { this.size=0; this.stackIndex=0; } public void push(int push) { if(size&lt;15) { stackPlaces[stackIndex]=push; size++; stackIndex++; } else { System.out.println("The stack is already full. Pop some elements and then try again"); } } public void pop() { if(size==0) { System.out.println("The stack is already empty"); } else { stackPlaces[stackIndex]=0; size--; stackIndex--; } } public void display() { System.out.println("The stack contains:"); for(int i=0;i&lt;stackPlaces.length-1;i++) { System.out.println(stackPlaces[i]); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload