Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse String to int with the default value? [duplicate]

I need to parse a string user id into integer for that I used Integer.parseInt(String s) but it returns null/nil, if there string has/holds non-decimal/integer value and in that case, I need to assign default integer value as 0.

I tried this but it (? 0) seems not working and I don't know whether it is correct syntax or not.

String userid = "user001";
int int_userid = Integer.parseInt(userid) ? 0;

How can assign default value to integer if there is null assignment?

String userid is a parameter argument as part of web service function call, so I cannot update it's data type to integer.

like image 914
Krunal Avatar asked Aug 03 '17 10:08

Krunal


People also ask

Can you parse a string to an int?

Use Integer.parseInt() to Convert a String to an Integer This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.

Which function is used to parse a string to int?

parseInt() The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

Which method accepts a string and converts it into integer?

The method generally used to convert String to Integer in Java is parseInt() of String class.

What is the method that can be used for converting string values into numeric values particularly integer?

Strings can be converted to numbers by using the int() and float() methods. If your string does not have decimal places, you'll most likely want to convert it to an integer by using the int() method.


2 Answers

You're most likely using apache.commons.lang3 already:

NumberUtils.toInt(str, 0);
like image 132
msfoster Avatar answered Oct 10 '22 12:10

msfoster


That syntax won't work for Integer.parseInt(), because it will result in a NumberFormatException

You could handle it like this:

String userid = "user001";
int int_userid;
try {
   int_userid = Integer.parseInt(userid);
} catch(NumberFormatException ex) {
   int_userid = 0;
}

Please note that your variable names do not conform with the Java Code Convention


A better solution would be to create an own method for this, because I'm sure that you will need it more than once:

public static int parseToInt(String stringToParse, int defaultValue) {
    try {
       return Integer.parseInt(stringToParse);
    } catch(NumberFormatException ex) {
       return defaultValue; //Use default value if parsing failed
    }
}

Then you simply use this method like for e.g.:

int myParsedInt = parseToInt("user001", 0);

This call returns the default value 0, because "user001" can't be parsed.

If you remove "user" from the string and call the method...

int myParsedInt = parseToInt("001", 0);

…then the parse will be successful and return 1 since an int can't have leading zeros!

like image 45
Yannick Avatar answered Oct 10 '22 11:10

Yannick