• Monday, September 15, 2014

    Java - Converting String to Integer or Integer to String.

    How to convert String to integer or integer to String in Java

    In Java, String is a predefined class in Java which can hold String Value(array of Characters.).
    If any string contain number such as "53", "21" , "110" and you want to use of Integer. It is not directly possible.

    You can convert it by using Integer.parseInt(myStringNumber).

    Example 1:

    String m="45";
    int a;
    a = Integer.parseInt(m);

    Example 2:

    public int StringToInt(String number)
    {
    return Integer.parseInt(number);

    }

    Converting integer to number is so easy. Just add empty string before "" any number like this:

    int a=5;
    String m;
    m= "" + a;

    Example 3:

    public String IntToString(int num)
    {
    return "" + num;
    }

    Syed Masoom Rizvi

    Author & Editor

    Hi, This is Syed Masoom Rizvi, who is continue exploring the world of programming ,programming and programming... :)

    0 comments:

    Post a Comment