1

I want to convert a primitive int to String with the following conditions :

Example

 input : int number = 474567;

convert into string with out using String class, Object class, no collection frame work.

Please let me know if this is possible with the above given condition. I am aware of all other methods i.e using collections and String class library.

Thanks in advance.

int digit = number;
int afterdivide = number;

String retValue = "";

while(afterdivide > 0) {
digit = afterdivide % 10;
afterdivide = afterdivide / 10;
if(digit == 0){
  retValue += "0";
}
else if(digit == 1){
  retValue += "1";
}
// Other numbers to follow
.
.
.
}

I tried something like this, but here i have created a String retValue and appending using '+' this will not satisfy my condition (String class used !!). A clue was given to me to solve this. Clue : How do you think String class is written in java.

2
  • 2
    Yes possible, now you try some code and share it if stuck Commented Dec 23, 2013 at 5:31
  • 1
    I suspect your homework involves modulo and ASCII. Or you're not explaining how you'd like us to do your assignment for you. Commented Dec 23, 2013 at 5:40

7 Answers 7

9

If no object methods should be used. This should work. Untested code but you get the idea.

int getlength(int x){
    while(x/10 != 0) length++;
    return length;
}
String convert(int x){
    char x_char = new char[getlength(x)];
    for(int i = x_char.length-1; i>=0; i--){
        x_char[i] = (char)(x % 10 + 48)//ASCII VALUE;
        x = x / 10;
    }
    return new String(x_char);
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Seems to fit the constraint "without using any available API"
Why the + 48 on your x_char[i] = (char)(x % 10 + 48)?
@EvilWashingMachine That is to convert an integer digit [0-9] to its character ASCII representation. If you look at the ASCII chart, the ASCII character '0' starts at 48, '1' at 49 and so on. asciitable.com
2

Yes possible.

String result = ""+474567;

But that's a hack. Don't use it unless you need it.

Another good way is (If there is allow to Object class)

new Integer(474567).toString();

2 Comments

Technically That's using a StringBuilder and the String class ;) I suspect his homework is to figure out modulo and char
new Integer(474567).toString(); <-- he can't use an "Object Class".
1

There are three ways that I know of:

String result = Integer.toString(number);

or

String result = String.valueOf(number);

or, and this one is the one that you need to use (because you can't use an "Object Class"):

String result = number + "";

1 Comment

+1 I'm a particular fan of String.valueOf(). Seem most semantically correct :)
0

As a hint, this will print it in reverse (it actually works for any base, not just 10):

public static void main(String...strings ){
    int num = 1234, base = 10;

    while(num >= 1){
        System.out.print(num%base);
        num /= base;
    }
}

Output: 4321

Comments

0

Try this. This is from toString method in Integer class.

public class Test
{
    final static char [] DigitTens = {
        '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
        '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
        '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
        '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
        '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
        '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
        } ; 

        final static char [] DigitOnes = { 
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        } ;

        final static char[] digits = {
            '0' , '1' , '2' , '3' , '4' , '5' ,
            '6' , '7' , '8' , '9' , 'a' , 'b' ,
            'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
            'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
            'o' , 'p' , 'q' , 'r' , 's' , 't' ,
            'u' , 'v' , 'w' , 'x' , 'y' , 'z'
            };

    final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };

    public static void main(String[] args) {
        try {
            int i = 1234;
            int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
            char[] buf = new char[size];
            getChars(i, size, buf);
            System.out.println(new String(buf, 0, size));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Requires positive x
    static int stringSize(int x) {
        for (int i = 0;; i++)
            if (x <= sizeTable[i])
                return i + 1;
    }

    static void getChars(int i, int index, char[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Generate two digits per iteration
        while (i >= 65536) {
            q = i / 100;
            // really: r = i - (q * 100);
            r = i - ((q << 6) + (q << 5) + (q << 2));
            i = q;
            buf[--charPos] = DigitOnes[r];
            buf[--charPos] = DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i <= 65536, i);
        for (;;) {
            q = (i * 52429) >>> (16 + 3);
            r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
            buf[--charPos] = digits[r];
            i = q;
            if (i == 0)
                break;
        }
        if (sign != 0) {
            buf[--charPos] = sign;
        }
    }
}

Comments

0
public class IntegertoString {
 
 public String s= "";
    // Method
    // Returning String from integer value
    public String tostring(int n)
    {
      while (n>0)
      {
          
          int num;
          num = n%10;
         
          s = ""+(num)+""+s;
          n= n/10;
     }
      return s;
    }
      
    // Method 2
    // main driver method
    public static void main(String[] args)
    {
        // Input integer value
        int n = 12340321;
        
        IntegertoString inttostring = new IntegertoString();
         
        System.out.println(inttostring.tostring(n));
         }
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1
((Integer)number).toString()

use this method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.