简体   繁体   中英

Parseint with leading zero Java

i've got a string, for example "1024", i need to remove one character from it as like this: 024 124 104 102

after i must find the minimum value, so, my minimum value is 024, and i want it to insert into an array, or Vector, whatever.

so, my code is:

void generateMin() {
        String number = "1024";

        Vector<Integer> variations = new Vector<Integer>();

        for (int i = 0; i < number.length(); i++) {
            String variation = number.replace(number.charAt(i), '\0');
            int numberVariation = Integer.parseInt(variation);
            variations.add(numberVariation);
        }
}

However, when i am running it i get an error:java.lang.NumberFormatException: For input string: " 024"

The problem is that when i am doing:

System.out.println(Integer.parseInt("024");

it returns 24, without errors, but there i am getting an error, so, can you help me? what is the problem?

The error specifies that the NumberFormatException is for " 024" since the character was not removed, try this to get the variation:

String variation = number.substring(0,i) + number.substring(i+1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM