-1

I am asking how to convert a string data type to an integer data type in Java. In Java, the String class represents a sequence of characters and the Integer class is used to represent integer values. Sometimes, it may be necessary to convert a String to an integer in order to perform arithmetic or other operations that require integer values. Suggest a solution or code snippet to accomplish this task in Java.

String myString = "123"; int myInt = (int) myString;

  • 1
    Does this answer your question? [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – beaker Mar 15 '23 at 14:47

1 Answers1

0

this is a function of a mine personal library:

public static int StringToInt(@Nullable final String daConvertire)
{
    int mV = 0;
    if(daConvertire != null && daConvertire.length() > 0){
        try{
            mV = Integer.parseInt(daConvertire);
        } catch (NumberFormatException nfe){
            try{
                if(daConvertire.endsWith(".0")){
                    mV = Integer.parseInt(daConvertire.replace(".0", ""));
                } else {
                    mV = Integer.parseInt(daConvertire.replace(".","").replace(",","."));
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    return mV;
}

This method returns an int (primitive). If you need an Integer, change return type value.