Java Integer.valueOf throws a NumberFormatException for a real number within a range

In Java, I make a call:

String chunkSizeAsString = responseString.split(DOUBLE_NEW_LINE)[1]
    .split(SINGLE_NEW_LINE)[0];
System.out.println("Trying to get integer value of '" + chunkSizeAsString + "'");
Integer chunkSize = Integer.valueOf(chunkSizeAsString, 16); // this is line 109

And we get the conclusion:

Trying to get integer value of '8d'
Exception in thread "Thread-2" java.lang.NumberFormatException: For input string: "8d"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.valueOf(Integer.java:556)
    at ProxyWorker.handleRequest(ProxyWorker.java:109)
    at ProxyWorker.run(ProxyWorker.java:41)
    at java.lang.Thread.run(Thread.java:745)

Basically, I call Integer.valueOf("8d", 16)and receive NumberFormatException. I saw many examples in which the OP forgot to indicate the correct base, or the resulting number was outside and Integer, Long, etc. But 0x8d = 141, which is convenient within the whole.

So my question is: why is this happening, and how can I fix it?

N.B. , chunkSizeAsString ( "8d" ) , . "\ u200e" "\ u200f", , "\\p {C}", , 109:

chunkSizeAsString = chunkSizeAsString.replaceAll("\u200e", "");
chunkSizeAsString = chunkSizeAsString.replaceAll("\u200f", "");
chunkSizeAsString = chunkSizeAsString.replaceAll("[^\\p{Print}]", ""); 

.

Edit: jdk1.7.0_7, - "Oracle Corporation". fge.

2: jdk 1.7u79 , Java- , , :

java version "1.7.0_79"

OpenJDK (fedora-2.5.5.0.fc20-x86_64 u79-b14)

64- OpenJDK ( 24.79-b02, )

3: , :

  • , Integer.valueOf("8d", 16) Integer.parseInt("8d", 16) pass
  • chunkSizeAsString.equals("8d")
  • chunkSizeAsString.length()= 2
  • - 56 100, ASCII "8" "d",
+4
3

, , (, ), , - .

:

private final AtomicInteger attemptCounter = new AtomicInteger(0);

void whateverYourMethodIsCalled(String responseString) {
    int attemptId = attemptCounter.incrementAndGet();
    System.out.format("Beginning attempt: %d%n", attemptId);
    String chunkSizeAsString = responseString.split(DOUBLE_NEW_LINE)[1]
        .split(SINGLE_NEW_LINE)[0];
    System.out.format("In attempt %d, trying to get integer value of '%s', which is length %d%n",
         attemptId, chunkSizeAsString, chunkSizeAsString.length());
    Integer chunkSize = Integer.valueOf(chunkSizeAsString, 16); // this is line 109
    System.out.format("Ending attempt: %d%n", attemptId);
}

, , , , , String - , .


: , , . , , , , , . , , @Tait / , , .

+3

, - . , F7 ( - ) Integer.valueOf(), , , .

+1

.valueOfuses the .parseIntbottom you can doint chunkSize = Integer.parseInt(chunkSizeAsString, 16);

0
source

Source: https://habr.com/ru/post/1584796/


All Articles