Home > Software design >  How can I loop through all the characters that are 0 in a given string?
How can I loop through all the characters that are 0 in a given string?

Time:09-21

I'm trying to remove trailing zeroes from an integer and here is my code so far.

import java.math.BigInteger;

public class newuhu {
    public static int numTrailingZeros(int s) {
        BigInteger J = BigInteger.valueOf(s);
        String sb = J.toString();
        String Y = "";
        while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '0') {
            sb.replaceAll("0"," ");


        }
        return Integer.parseInt(Y);
    }

Note: I turned my int into a Biginteger because I've been warned that some inputs may look like 20!, which is 2.432902e 18

However, my IntelliJ debugging tool tells me that variable sb isn't in the loop. So, I'm trying to understand what must be done to make sure sb is in the loop.

Please understand that I'm a beginner in Java so, I'm trying to learn something new.

CodePudding user response:

replaceAll replaces all occurrences of string with character that you want (ie space) so you don't need loop at all, also you're concerned about overflow so you should actually use BigInteger as a parameter, not int (int wont fit anything close to 20!) but there's another issue with your code, you said you want to replace trailing zeros but right now you will replace every 0 with blank character, you should try to use something like https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

public class newuhu {
public static int numTrailingZeros(BigInteger s) {
    String sb = s.toString();
    return Integer.parseInt(sb.replaceAll("0", "")); // consider returning something else if you're working with BigInteger
}

CodePudding user response:

Keep in mind that when doing BigInteger.valueOf(int) does not have an effect as a number to big for int will never be stored in an int. Also 20! is fine for int.

public static String trimTrailingZeros(String source) {
    for (int i = source.length() - 1; i > 0;   i) {
        char c = source.charAt(i);
        if (c != '0') {
            return source.substring(0, i   1);
        }
    }
    return ""; // or return "0";
}

Or if you prever BigInteger.

public static BigInteger trimTrailingZeros(BigInteger num) {
    while (num.remainder(BigInteger.TEN).signum() == 0) {
        num = num.divide(BigInteger.TEN); 
    }
    return num;
}

This should be fast as you only create one string (via substring).

CodePudding user response:

(First: variables and fields should start with a small letter - when no constants.)

It should be

sb = sb.replaceAll(...)

as sb is not changed by replaceAll, only the replaced value is returned. The class String gives immutable values, that always remain the same, so you can assign a variable to a variable and changing values of either variable will never influence the other - no further sharing.

Then it should be:

sb = sb.replaceFirst("0$", "");

replaceAll would replace every 0, like "23043500" to "23435".

replaceFirst replaces the _regular expression: char '0' at the end $.

Overflow on the input number is not possible, as you are already passing an int.

public static int numTrailingZeros(int n) {
    while (n != 0 && n % 10 == 0) {
        n /= 10;
    }
    return n;
}

public static int numTrailingZeros(String n) {
    n = n.replaceAll("0 ", "");
    if (n.isEmpty() || n.equals("-")) { // "-0" for pessimists?
       n = "0";
    }
    return Integer.parseInt(n);
}

% is the modulo operator, the remainder of an integer division 147 % 10 == 7.

The name is misleading, or you are calculating something different.

public static int numTrailingZeros(int n) {
    int trailingZeros = 0;
    while (n != 0 && n % 10 == 0) {
        n /= 10;
          trailingZeros ;
    }
    return trailingZeros ;
}

CodePudding user response:

The problem here is that sb.replaceAll("0","") won't do anything. You're throwing away the return value that contains your replaced string. See here.

What you probably want is something like this:

while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '0') {
    sb = sb.replaceAll("0"," ");

I'm not sure you need a while loop, though. ReplaceAll will... replace all of the zeros with spaces.

  • Related