Java Conversions
How to convert Java String to Int ?
Whenever we receive data as string and it has data in number format ,if we want to perform mathematical operation the string then we need to convert string to int
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class StringToInteger { public static void main(String[] args) { /* * Convert a string to an integer with the parseInt method of the Java Integer class. * The parseInt method is to convert the String to an int and throws a NumberFormatException, * if the string cannot be converted to an int type. * */ //String str1="fred"; String str1="12980"; try{ int num1 = Integer.parseInt(str1); System.out.println("Number is = "+num1); }catch(NumberFormatException NF){ System.out.println("NumberFormatException "+ NF.getMessage()); } } } |
How to Convert Int to String
1 2 3 4 5 6 7 8 9 10 |
public class IntergerToString { public static void main(String[] args) { int i=14567; String str1 = String.valueOf(i); System.out.println("String is = "+str1); } } |