Java String to Integer Example . Use of Integer.ValueOf(), parseInt()

/* Many times , We may come accross a situation where a string needs to be changed to an Integer . For example when we accept a input through keyboard , it will be read as String . For doing any calculation , we have to convert the string to an integer . Integer Object has methos like valueOf & parseInt for converting sting to integer.
We can convert String to Integer using the following ways

Integer.valueOf - converts String to Integer Object
Integer.parseInt - converts String to int primitive type
If we pass a string to the constructor of the Integer object , it returns Integer Object. */



import java.io.*;
public class StringToInt {
public static void main(String[] args) {

Example1();
Example2();
}

// Example 1:

private static void Example1()
{

System.out.println("Example 1 ");
String numStr ="110";
Integer number = new Integer(numStr);

String numStr1="0005";
int number1 = Integer.valueOf(numStr1);

String numStr2="0007";
int number2=Integer.parseInt(numStr2); //converts to integer primitive type.

System.out.println( "Number String is ( string concatenation)" + numStr+numStr2+numStr2);

int result=number+number1+number2;
Integer result1=number+number1+number2;

System.out.println("Input Numer" + number);
System.out.println("Input Numer1" + number1);
System.out.println("Input Numer2" + number2);

System.out.println( "After Converting Integer ( string concatenation) " + number+number1+number2);
System.out.println( "result " + result);
System.out.println( "result1 " + result1);

}

// Example 2:

private static void Example2()
{

BufferedReader br=new BufferedReader (new InputStreamReader(System.in) );

String numStr="";
int number=0;
int sum=0;

System.out.println("Example 2 ");
System.out.println("Enter Numbers to add & type quit to exit ");

do
{

try
{
numStr=br.readLine();
number = Integer.parseInt(numStr);
}
catch (Exception e)
{
if (numStr.equalsIgnoreCase("quit") )
{
System.out.println("Quiting....");
number=0;
}
else
{
System.out.println("Invalid Input");
number=0;
}
}
sum = sum + number;
} while (numStr.compareTo("quit") !=0);

System.out.println("Sum is =" +sum);
}

}



Output :
--------

D:\as2\JF1>java StringToInt
Example 1
Number String is ( string concatenation)11000070007
Input Numer110
Input Numer15
Input Numer27
After Converting Integer ( string concatenation) 11057
result 122
result1 122
Example 2
Enter Numbers to add & type quit to exit
7
5
3
7
2
quit
Quiting....
Sum is =24

1 comments:

javin paul said...

interesting article just to add you can even convert binary , octal or hexadecimal numbers from String to int by passing redix to Integer.parsetInt() method as shown below :
-----------------------------------------------------------------------
static int parseInt(String s, int radix)
Parses the string argument as a signed integer in the radix specified by the second argument.
-------------------------------------------------------------------------
Thanks
Javin
How to convert String to Int to String in java with example

Post a Comment

 
Copyright 2011 Java Online