kh14
kh14

Reputation: 19

Remove leading zeros in an int array

Attempt to merge numbers by removing the leading zeros in the Int array.

int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
for (int i = 0; i < arr.length; i++) {
    String.format("%09d", array[i]);
}

for (int i = 0; i < arr.length; i++) {
    System.out.print(array[i]);
}

Desired output:

405

Upvotes: 0

Views: 3752

Answers (7)

Mohammed Al-Tamimi
Mohammed Al-Tamimi

Reputation: 37

    int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };

    int i = 0;
    for (; i < array.length; i++) {
        if (array[i] > 0)
            break;
    }

    for (; i < array.length; i++) {
        System.out.print(array[i]);
    }

Upvotes: 1

LuCio
LuCio

Reputation: 5173

Since an array has a fixed size you cannot remove any element from an array. Thus I assume you want to have a new array without the leading zeros.

With Java 9+ (dropWhile was not present in Java 8) you can do it this way:

int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
int[] withoutLeadingZeros = Arrays.stream(array).dropWhile(i -> i == 0).toArray();
System.out.println(Arrays.toString(withoutLeadingZeros)); // [4, 0, 5]

EDIT
In case the result is intended to be a String:

String withoutLeadingZeros = Arrays.stream(array).dropWhile(i -> i == 0).collect(StringBuilder::new, StringBuilder::append, (l, r) -> l.append(r)).toString();
System.out.println(withoutLeadingZeros); // 405

EDIT
In case the result is intended to be an int:

int number = Arrays.stream(array).reduce((l, r) -> l * 10 + r).getAsInt();
System.out.println(number); // 405

Upvotes: 6

The Scientific Method
The Scientific Method

Reputation: 2436

try this,if you want to print:

int[] arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
boolean isNotLeadingZero=false;
for (int i = 0; i < arr.length; i++) {
    if((arr[i] != 0)  || isNotLeadingZero){
        System.out.print(arr[i]);
        isNotLeadingZero= true;
    }
}

output

405

Upvotes: 0

StephaneM
StephaneM

Reputation: 4899

Convert your array into a number then print it:

    int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };
    int exp = 1;
    long total =0;
    for (int i = array.length -1; i >=0; i-- ) {
        total += array[i]*exp;
        exp *=10;
    }


    System.out.println(total);

Then you can even use the result for further computations.

Upvotes: 0

anupam691997
anupam691997

Reputation: 310

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

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5 };

        int i,j;

        for (i = 0; i < arr.length; i++) {
            if(arr[i]!=0)
                break;
        }

        j=0;

        for(;i<arr.length;i++)
        {
            arr[j]=arr[i];
            j++;
        }



        for (i = 0; i < j; i++) {
            System.out.print(arr[i]);
        }
    }
}

i have shifted the elements as you asked to remove the leading zeroes

Upvotes: 0

NiVeR
NiVeR

Reputation: 9786

You can use this code:

boolean foundNonZero = false;
for (int i = 0; i < arr.length; i++) {
    foundNonZero |= (arr[i] != 0);
    if(foundNonZero) {
       System.out.print(array[i]);
    }
}

Upvotes: 0

robertobatts
robertobatts

Reputation: 967

You can do it by inserting a boolean.

  boolean leadingZeroFinished=false;
  for (int i = 0; i < arr.length; i++) {
       if(array[i]!=0) {
           leadingZeroFinished=true;
       }
       if(leadingZeroFinished) {
          System.out.println(array[i])
       }
  }

If you also want to remove the element, you better use a List

Upvotes: 1

Related Questions