In this tutorial we see how to convert string to int.
Generally when we enter int values in text filed, text area and submit the form, the parameters we will receive like as a string format in server side. In that case we need to convert those values in int format and perform the operations.
we have two methods to use to convert string to int in java.
First one :
Integer.parseInt() method is use to convert string to int in java.
Integer.valueOf() returns primitive int.
Let see the code example of Integer.parseInt().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class StringtoIntexample { public static void main(String args[]){ String s="1000"; // Converting string to int int ivalue=Integer.parseInt(s); System.out.println(ivalue) ; } } |
OutPut:
1 2 3 4 5 |
1000 |
Second approach:
Integer.valueOf() method is use to convert string to int in java.
Integer.valueOf() method returns Integer Object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.connect2java.javainterviewprograms; public class StringToIntegerExample { public static void main(String[] args) { // TODO Auto-generated method stub String stingvalue="1000"; Integer i=Integer.valueOf(stingvalue); System.out.println(i); } } |
OutPut:
1 2 3 4 5 |
1000 |