0

this is my code can i print my array without zero if it is empty?

import java.util.Arrays;
import java.io.*;
public class Stacks{
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("what is the size of your stack? :");
        int size = Integer.parseInt (br.readLine());
        int get = size;
        int[] Array = new int[size];
        System.out.println("type: push , pop , exit");
        System.out.println("remember! you can EXIT anytime");
        System.out.println(Arrays.toString(Array));
/*there still a code here but this is just what i needed to show..*/
    }
}

please help me.. PS I don't want to import stacks..

6
  • What do you mean by without zero? If your array is empty this will print [] not 0. Commented Oct 25, 2015 at 1:06
  • What value do you expect to find in the array? Each int will have some value. Commented Oct 25, 2015 at 1:07
  • if i input the size of 5 it will print like this [ 0 0 0 0 0] Commented Oct 25, 2015 at 1:15
  • can i print it like this? [] Commented Oct 25, 2015 at 1:16
  • 1
    @Hello23, An empty array is one with size 0. It will print out as []. If you have an array of size 5 then it contains 5 ints, which are initialized to 0. It appears intend to treat the array as a stack in the rest of your code, but then what happens if users put 0 on your stack. Do those 0s need to be printed or not? Commented Oct 25, 2015 at 1:28

3 Answers 3

1

Since your not using your Array as an array, but ultimately as a stack, you will not want to use Arrays.toString() which is designed for printing arrays as arrays. You need to write your own method, bearing in mind the stack your creating maybe smaller than the size of the array you're populating.

Without knowing how you're stack is implement, a basic model would be

public static String arrayAsStack(int[] array, int elements_in_stack) {
   String out = "[";
   for(int i=elements_in_stack-1; i>=0; i--)
      out += arrary[i] + " ";
   out+="]";
   return out;
}

This method of course may not be right, depending on the way you format your stack-array. Note the elements_in_stack should start at 0. Once you get the right implementation of this method for you stack you can just print the results in the natural way.

Sign up to request clarification or add additional context in comments.

Comments

0

if you just want your array to be displayed without the zeros you can just use a if statement.

for( int i = 0; i < Array.length; i ++ ){
    if( Array[i] != 0 ){
        System.out.print( Array[i] + " ");
    }
}

This will just iterate through your array and test if the value is zero. If it is not it will display the value, if it is, it disregards it.

2 Comments

sir if i input zero i also need to display it
then to answer your question, no you can't print your array as [ ] if it is empty.
0

instead of using int type i created it using object

 import java.util.Arrays;
 import java.io.*;
  public class Stacks{
public static void main(String[] args)throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("what is the size of your stack? :");
    int size = Integer.parseInt (br.readLine());
    int get = size;
    Object[] Array = new Object[size];
    System.out.println("type: push , pop , exit");
    System.out.println("remember! you can EXIT anytime");
       /* here i use the code of Ryan*/ 
      for( int i = 0; i < Array.length; i ++ ){
         if( Array[i] != null ){
              System.out.print( Array[i] + " ");
             }
        }
 /*there still a code here but this is just what i needed to show..*/
   }
}

now i can display 0 if I input it..

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.