Hello23
Hello23

Reputation: 37

can I print empty int array withou zero aprearing in java

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..

Upvotes: 0

Views: 4207

Answers (3)

Linus
Linus

Reputation: 894

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.

Upvotes: 1

Hello23
Hello23

Reputation: 37

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..

Upvotes: 0

Ryan
Ryan

Reputation: 1974

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.

Upvotes: 0

Related Questions