top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert an integer to string in Java without use of any inbuilt function?

+1 vote
2,200 views
How to convert an integer to string in Java without use of any inbuilt function?
posted Mar 14, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

Here is compiled program which return String of any integer.

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    private static int number;
    private static String stringresult;
    public static void main (String[] args) throws java.lang.Exception
    {
    System.out.println("Int To String");
    number=7667868;
    stringresult=IntToString(number);
    System.out.println("Number is To String="+stringresult);

    }
    public static String IntToString(int number)
{
    int StringConvet = 48;

    int eachDigit = number;
    int afterDivide = number;
    String reVal = "";

    while(afterDivide >0)
    {
        eachDigit = afterDivide % 10;
        afterDivide = afterDivide / 10;
        if(eachDigit == 0)
        {
            reVal += "0";
        }
        else if(eachDigit == 1)
        {
            reVal += "1";
        }
        else if(eachDigit == 2)
        {
            reVal += "2";
        }
        else if(eachDigit == 3)
        {
            reVal += "3";
        }
        else if(eachDigit == 4)
        {
            reVal += "4";
        }
        else if(eachDigit == 5)
        {
            reVal += "5";
        }
        else if(eachDigit == 6)
        {
            reVal += "6";
        }
        else if(eachDigit == 7)
        {
            reVal += "7";
        }
        else if(eachDigit == 8)
        {
            reVal += "8";
        }
        else if(eachDigit == 9)
        {
            reVal += "9";
        }
    }
    String reVal2 = "";
    for(int index =  reVal.length() -1 ; index >= 0 ; index--)
    {
        reVal2 += reVal.charAt(index);
    }
    return reVal2;
}
}
answer Mar 14, 2016 by Shivam Kumar Pandey
here is the link of online successfully compiled code: https://ideone.com/GgRvoM
Similar Questions
+2 votes

I want to convert a string say "98989" into an integer without using any library functions in java. Give fastest way to do it and explain why your method is best.

+3 votes

Given input as a string, return an integer as the sum of all numbers found in the string

Input: xyzonexyztwothreeeabrminusseven
Output : 1 + 23 + (-7) = 17

...