# How to convert INT to String.

• March 6th, 2014, 12:32 PM
wkqktlfj
How to convert INT to String.
Hi yall.
Im trying to convert Number into String
For ex.

I used 6 eggs out of the 12 for the cake

-> I used six eggs out of the 12 for the cake

Trying to convert the numbers (only 0~9)

public Converter(String startingString)
{
original = new String(startingString);
workingOnIt = new StringBuilder(startingString);
}
public void convert( )
{
int index;
for(index =0; index < original.length(); index++)
{

}
can anyone help.
• March 6th, 2014, 12:43 PM
KevinWorkman
Re: How to convert INT to String.
What have you tried?

Have you learned about arrays yet?
• March 6th, 2014, 12:55 PM
wkqktlfj
Re: How to convert INT to String.
I tried

int[] number = {1,2,3,4,5,6,7,8,9};
if(original.length() == number)
{

}

but i dont know how to replace them.
• March 6th, 2014, 01:05 PM
Norm
Re: How to convert INT to String.
The array was to hold the Strings for the names of the numbers
and the number was used to index into the array.
For example the first element in the array would be "zero"
• March 7th, 2014, 02:13 AM
zkidkid
Re: How to convert INT to String.
Quote:

Originally Posted by wkqktlfj
I tried

int[] number = {1,2,3,4,5,6,7,8,9};
if(original.length() == number)
{

}

but i dont know how to replace them.

First, you need an array to map number to string, like this:
Code :

`String[] number = {"zero","one","two","three","four","five","six","seven","eight","nine"};`

And then you need to split your string to list of word. (token your string)
Code :

`String[] words = your_data.split(" ");`

After that you need to go through your words & check if there has any number from 0->9
Code :

```for(String word : words){ //check word & replace here }```
Ok, by now you need to check if it's number & replace it.
Code :

```if(word.lengh() == 1){ char c = word.charAt(0); if(c >= '0' && c <='9') return number[c - '0']; }```

Incase you know ascii code, you code compare like that:

Code :

```int charCode = word.charAt(0); if( charCode >= 48 && charCode <= 57){ return number[charCode - 48]; }```

I think all would be clearly now, you need to think about it again & again and do coding by your self.
If you need any help, just post your code then I & others would happy to help you.

Happy Coding \:D/