Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
Convert String To int Java - Examples | Java
Latest :

Convert String To int Java – Examples | Java

How to Convert String to int in Java – The following article will let you know the complete information about string to int java, in case if you have any doubts do let me know.

We have different ways to convert a String to int in Java

  • Integer.parseInt()
  • Integer.valueOf()
  • Integer.parseUnsignedInt()

Our own logic equivalent to – Integer.parseInt()

  • Integer.decode()
  • Integer() constructor

Etc..

String To Int Java

Integer.parseInt() – How It Works:

Syntax1 – int parseInt(String);

Example:

It converts the given String into an integer. Suppose we give a String “234” as argument to the method, then it returns the integer 234. As this is a static method, we can invoke this using its class name.

Example:

Algorithm for converting string to int without built-in method:

Implementation:

Output:

Integer.decode() – How It Works

An empty String or a String that contains non-digits will result in Number Format Exception.

Ex:    

“234A”, “34.54”, “smile”, “”, etc.

Syntax2:               int parseInt(String,int);

This is similar to normal parseInt(), but it takes an integer as second argument and this argument represents the radix of the content in the input String.

Suppose we want to convert an octal number (which is in String format) into its equivalent decimal integer then the second argument should be 8.

Similarly, if we want a hexa-decimal String to its decimal number then the second argument should be 16.

Eg:            Integer.parseInt(“234”,10);  will result in 234

                  ===>  2*(10^2)+3*(10^1)+4*(10^0)

                  ===>  (2*100)+(3* 10)+(4*1)

                  ===>  200+30+4

                  ===>    234

Implementation:

Example:

 Integer.parseInt(“234”,2);  will result in NumberFormatException as allowed digits in binary(2) number                        system are 0 and 1 only.

 Integer.parseInt(“789”,8);  will result in NumberFormatException as allowed digits in octal(8) number                           system are from 0 to 7 only

string to int java – Integer.valueOf()

It can also be used similar to Integer.parseInt() to convert a String to int. This method also has an overloaded method to take an int (radix) as second argument where as the first argument is a String.

Integer.parseInt() returns the result as a basic (primitive) int type where as Integer.valueOf() returns the result an Integer object.

Example:       

  • Integer.valueOf(“234”); will give 234 as Integer object
  • Integer.valueOf(“412”); will give 412 as Integer object
  • Integer.valueOf(“234”,8); will give 156 as Integer object
  • Integer.valueOf(“234”,16); will give 564 as Integer object

Integer.parseUnsignedInt()

This is also similar to Integer.valueOf() and Integer.parseInt() but it can work on positive values only.

Example:         

  • Integer.parseUnsignedInt(“874”); returns 874         
  • Integer.parseUnsignedInt(“+874”); returns 874          
  • Integer.parseUnsignedInt(“-874”); raises NumberFormatException

Algorithm:

Syntax: Integer decode(String);

This method converts the given string into an int and returns an Integer object.

Example:

The decode() method can assume the type (decimal, octal, hexadecimal…) of the value and makes the conversion accordingly. In computer parlance, a number preceded by 0 is treated as octal and a number

Example:

Convert String To int java – Integer()

The class Integer has several constructor and one of them receives a String as argument. By using this, we can convert a String to Integer object. Once we have an integer object, we can convert it to an int (primitive) type.

Example:

If you have any doubts related to Convert Java String To Int do leave a comment here.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
x

Check Also

What is Recursion In Java Programming – JavaTutoring

What is Recursion In Java programming – Here we cover in-depth article to know more ...