NumberFormatException making parse

2

I have a method that receives a series of parameters, one of them is a value of type Float and when trying to cast it to a int it gives me failure and cascates the application. The error occurs in the third row of the method, where I declare cantidadIntroducidoI .

Method:

public void cambioCompraValores(Float cantidadValores, int mediaInt) {

    Float cantidadIntroducidoF = cantidadValores;
    String cantidadIntroducidoS = String.valueOf(cantidadIntroducidoF);
    int cantidadIntroducidoI = Integer.parseInt(cantidadIntroducidoS);
}

Error:

FATAL EXCEPTION: main
                                                                         Process: com.example.iberd.actionvalue, PID: 9292
                                                                         java.lang.NumberFormatException: For input string: "36.26"
                                                                             at java.lang.Integer.parseInt(Integer.java:521)
                                                                             at java.lang.Integer.parseInt(Integer.java:556)
                                                                             at com.example.iberd.actionvalue.ScreenSlidePageFragment.cambioCompraValores(ScreenSlidePageFragment.java:363)
                                                                             at com.example.iberd.actionvalue.ScreenSlidePageFragment.calculoValores(ScreenSlidePageFragment.java:289)
                                                                             at com.example.iberd.actionvalue.ScreenSlidePageFragment.access$200(ScreenSlidePageFragment.java:46)
                                                                             at com.example.iberd.actionvalue.ScreenSlidePageFragment$1.onClick(ScreenSlidePageFragment.java:202)
                                                                             at android.view.View.performClick(View.java:5637)
                                                                             at android.view.View$PerformClick.run(View.java:22429)
                                                                             at android.os.Handler.handleCallback(Handler.java:751)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    
asked by Eduardo 29.05.2017 в 12:13
source

3 answers

2

The problem you're having is that you can not parse an entire decimal number with the function Integer.parseInt

This can be solved in several ways:

- Round () : You can use the Math.round() function that will return the integer closest to the round number. Either greater or lesser. Example:

float f = 100.6;
Int i = Math.round(f); // Devolverá 101

- Floor () : You can use the Math.floor() function that will return the lower integer closest to the number to be rounded.

float f = 100.6;
Int i = Math.floor(f); // Devolverá 100

- Ceil () : You can use the Math.ceil() function that will return the top integer closest to the number to be rounded.

float f = 100.6;
Int i = Math.ceil(f); // Devolverá 101

On the other hand, as @ SJuan76 says, you have a function of the class Float that returns the entire value of a float, this function is intValue() that behaves similar to the function Math.round()

    
answered by 29.05.2017 / 12:30
source
2

The problem is that you get the representation of a Float as String , which can be something like "3.141592" or even "6.28 + E28".

None of the above values can be converted directly to integer, and valueOf will reject anything that is not a representation in String of an int.

There are two options:

  • Analyze the representation of the String. With indexOf and substring , find the parts of the String and delete those that remain to pass a "clean" value to valueOf . You will have to take into account, of course, the value of the exponent if there is one.

  • Read the javadoc of the Float class , and do:

    cantidadValores.intValue()
    

    If it seems too simple and / or you want to cast, you can also do

    float valorFloat = cantidadValores;
    int cantidadIntroducidoI = (int) valorFloat.
    

I recommend the second option ...

    
answered by 29.05.2017 в 12:29
2

The message defined in the LogCat indicates the problem:

  

java.lang.NumberFormatException: For input string: "36.26"

you are trying to make parse whole of a value type String that contains a value that can only be parsed to float.

In this case the ideal is to round the value to the nearest integer by using the round () , which rounds a value float or doble to the nearest integer value. :

Math.round ()

your method would be simply (I suggest adding exception handling):

  public void cambioCompraValores(Float cantidadValores) {
        try {

            int cantidadIntroducidoI = Math.round(cantidadValores);

        }catch(NumberFormatException nfe){
            Log.e("Error" , "NumberFormatException " + nfe.getMessage());
        }
    }
    
answered by 30.05.2017 в 01:31