Error conversion String-Integer in Java

0

I have a problem in java and I would greatly appreciate your help.

I am developing a project in which we are working with several colleagues, we also use functionality that comes from outside. The fact is that I have to make the step from String to Integer.

Ilustro with the method used for the conversion;

private int parseoint(String aux) {
    aux.replaceAll(" ", "");
    int numEntero = Integer.parseInt(aux);
    return numEntero;
}

And this one used to narrow the String to convert;

if (matChu.find()) {
    this.chunk = matChu.group(1);
    this.chunk_parsed = parseoint(this.chunk);
} //....Aqui el codigo sigue

With the first we convert what we get out of the second. The problem is that this is the result obtained;

Exception in thread "Thread-1" Exception in thread "Thread-2" > queryjava.lang.NumberFormatException: For input string: "1.0"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)

The debugger and others have been used, but now it has been a couple of days without finding the error. A cordial greeting and thanks for your time.

    
asked by Alejandro Cano 27.11.2017 в 18:40
source

1 answer

1

For what I am seeing you are trying to carry a floating number or double to whole number. For this you only have to delete the decimal part. In this case the value is given by a String but I suggest you make these changes so that you get the whole value of the number in question.

public int conversor(String numero){
    numero = numero.replace(".", ";"); //remplazamos el separador por otro temporalmente
    System.out.println("respuesta:"+numero.split(";")[0]+" - "+numero.split(";")[1]);

  return Integer.valueOf(numero.split(";")[0]); //luego regresamos la parte entera del numero
}
    
answered by 27.11.2017 в 21:07