Java program to convert Integer to String

Hi Everyone, this post deals with the conversion of Integer to String. Please refer my earlier post which deals in converting String to Int.

The logic of the program is very simple. We have to divide the given number by 10 and append the remainder to a String until the number reaches 0.

The program is as follows:

public class program {
    public static void main (String args[])
    {
        int num = 52;
        String str = IntToStr(num);
        System.out.println(\" The string representation of the number is : \"+str);
    }
    
    public static String IntToStr (int num)
    {
        boolean isNegative = false;
        if(num <0 )      
        {
            num = num* -1;
            isNegative=true;
        }
        StringBuilder build = new StringBuilder();
        if(isNegative)
        {
            build.append(\'-\');
        }
        StringBuilder build1 = new StringBuilder();
        
        while(num!=0)
        {
            int temp = num%10;
            build1.append(temp);
            num=num/10;
        }
        build1.reverse();
        build.append(build1);
        return build.toString();
    }

}


The output of the program is as follows:

The string representation of the number is 52


I believe everyone liked this post. Please comment below if you have doubts. All doubts will be responded soon.

Thank you all for reading the blog post.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++