简体   繁体   中英

NumberFormatException when I try to convert a 3 character string into integer

"Exception in thread "main" java.lang.NumberFormatException: For input string: "l20""

this is the error message i received for trying to typecast a 3 digits string to int.

If i understood correctly the max value for int in java is 2147483647?

This is the method that caused the syntax

private int getRed(String key) {
    return Integer.parseInt(key.substring(3,6));
}

Edit: for clarification the key is a 12 bit string randomly generated by the following code

for(int i=0;i<12;i++) {
      Random random = new Random();
      key=key+Integer.toString(random.nextInt(10));
}

Edit 2:below is a minimal reproducible example and it produces this error message "Exception in thread "main" java.lang.NumberFormatException: For input string: "l35" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Test.getRed(Test.java:57) at Test.getKey(Test.java:44) at Test.encode(Test.java:36) at Test.main(Test.java:70)"

public class Test {
    String key; 
    public Test() {
        for(int i=0;i<12;i++) {
            Random random = new Random();
            key=key+Integer.toString(random.nextInt(10));
        }
    }

    public void encode() {
        for(int i=0; i<5;i++) {
            int key=getKey(i);
        }
    }
    private int getKey(int i) {
        int indicator = i%3;
        int returnInt=0;
        switch (indicator) {
        case 0:
            returnInt=getRed(key);
            break;
        case 1:
            returnInt=getGreen(key);
            break;
        case 2:
            returnInt=getBlue(key);
            break;
        }
        return returnInt;
    }

    private int getRed(String key) {
        return Integer.parseInt(key.substring(3,6));
    }

    private int getGreen(String key) {
        return Integer.parseInt(key.substring(6,9));
    }

    private int getBlue(String key) {
        return Integer.parseInt(key.substring(9,11));
    }

    public static void main(String args[]) {    
        Test test=new Test();
        test.encode();
    }
}


Perhaps compare your code to mine. I pretty much did it the way you said, with only the slightest differences.

$ javac Num.java && java Num
Full key: 255142125179
Parsing: 142
Parsed: 142
$ cat Num.java
import java.util.Random;

public class Num {
    public static int getRed(String key) {
        System.out.printf("Parsing: %s\n", key.substring(3, 6));
        return Integer.parseInt(key.substring(3,6));
    }

    public static void main(String[] args) {
        String key = new String("");
        Random random = new Random();

        for (int index = 0; index < 12; ++index) {
            key = key + Integer.toString(random.nextInt(10));
        }

        System.out.printf("Full key: %s\n", key);
        int value = getRed(key);
        System.out.printf("Parsed: %d\n", value);
    }

}

I don't see a problem. It appears to have worked properly, so you didn't include something important.

"Exception in thread "main" java.lang.NumberFormatException: For input string: "l20""

this is the error message i received for trying to typecast a 3 digits string to int.

If i understood correctly the max value for int in java is 2147483647?

This is the method that caused the syntax

private int getRed(String key) {
    return Integer.parseInt(key.substring(3,6));
}

Edit: for clarification the key is a 12 bit string randomly generated by the following code

for(int i=0;i<12;i++) {
      Random random = new Random();
      key=key+Integer.toString(random.nextInt(10));
}

Edit 2:below is a minimal reproducible example and it produces this error message "Exception in thread "main" java.lang.NumberFormatException: For input string: "l35" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Test.getRed(Test.java:57) at Test.getKey(Test.java:44) at Test.encode(Test.java:36) at Test.main(Test.java:70)"

public class Test {
    String key; 
    public Test() {
        for(int i=0;i<12;i++) {
            Random random = new Random();
            key=key+Integer.toString(random.nextInt(10));
        }
    }

    public void encode() {
        for(int i=0; i<5;i++) {
            int key=getKey(i);
        }
    }
    private int getKey(int i) {
        int indicator = i%3;
        int returnInt=0;
        switch (indicator) {
        case 0:
            returnInt=getRed(key);
            break;
        case 1:
            returnInt=getGreen(key);
            break;
        case 2:
            returnInt=getBlue(key);
            break;
        }
        return returnInt;
    }

    private int getRed(String key) {
        return Integer.parseInt(key.substring(3,6));
    }

    private int getGreen(String key) {
        return Integer.parseInt(key.substring(6,9));
    }

    private int getBlue(String key) {
        return Integer.parseInt(key.substring(9,11));
    }

    public static void main(String args[]) {    
        Test test=new Test();
        test.encode();
    }
}


As I see you try from " l 20" get integer by Integer.parseInt(key.substring(3,6)); As I know parseInt() parse integers (numbers). What number is "l"?) I don't know and Integer do not know, that's why it throws this exception)

I just figured out what was wrong I didnt initialize String key to "" so it defaulted to null and the substring 3-6 it was trying to convert started with an L

once I fixed it the error is gone

Thank you everyone who helped

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