0

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
3
  • Is your Output the desired one or the one you currently get and don't want? By the way, this is not a working example... Commented Oct 9, 2018 at 12:27
  • 1
    Your first loop does nothing (useful). Commented Oct 9, 2018 at 12:28
  • 3
    Do you mean remove the zeros from the array, or print the array without the leading zeros? Very different questions with different solutions! Commented Oct 9, 2018 at 12:31

7 Answers 7

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

I've updated my answer showing how to get a String or an int result.
1

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

Comments

1
    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]);
    }

3 Comments

Does this work (compile)? Is i known in the second loop? I haven't ran it but it looks like it will just print the array as is?
this will not compile, check it first.
Yes it works, it's not necessarily to write the initialization inside for block, you can write it as following: i=0; for (;;){ if (i<size) break; ... ... ... i++; }
0

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]);
    }
}

Comments

0
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

Comments

0

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.

Comments

0

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

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.