Re: converting int to str
Integer.toString(intVariable)
Or do you actually mean rendering the number in words? There's no standard way of doing that in Java (or any other language I'm aware of, with the exception of one whose name I can't remember - could be Python).
You'd do that using lookup tables.
Re: converting int to str
can you give me an example using the "Integer.toString(value)"
Re: converting int to str
Code:
int i=1;
String s;
s=Integer.toString(i);
But that only parses (converts) Integer to String.
Re: converting int to str
i dont know really how to use the "String s = String.newValue() and String = "" + i"
i use this but it always says "outofbound"
import java.util.*;
import java.io.*;
public class convert
{
String [] h1 = {"","one hundred"};
String [] h2 = {"","two hundred"};
String [] h3 = {"","three hundred"};
String [] h4 = {"","four hundred"};
String [] h5 = {"","five hundred"};
String [] h6 = {"","six hundred"};
String [] h7 = {"","seven hundred"};
String [] h8 = {"","eight hundred"};
String [] h9 = {"","nine hundred"};
String [] t = {"and","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
String [] d = {"", "one", "two","three","four","five","six","seven","eight","nine"};
String [] dx = {"", "eleven","twelve","thirteen","fourteen","sixteen","seventeen","eighteen","nineteen"};
public convert(int num)
{
String result = "";
int h1d,h2d,h3d,h4d,h5d,h6d,h7d,h8d,h9d,td,dd;
h1d = td = dd = 0;
h2d = td = dd = 0;
String ds = String.valueOf(num);
h1d = Integer.parseInt(ds.substring(0,1));
h2d = Integer.parseInt(ds.substring(1,2));
td = Integer.parseInt(ds.substring(2,3));
dd = Integer.parseInt(ds.substring(3,4));
result += h1[h1d];
result += h2[h2d];
result += (" "+ t[td]);
result += (td !=1) ? (" "+ d[dd]) : dx[dd];
System.out.println(result);
}
public static void main(String args[])
{
String input = "";
try
{
BufferedReader woof = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
System.out.println("Enter 3 digits only: ");
input = woof.readLine();
if (input.equals("q"))
{
break;
}
int n = Integer.parseInt(input);
if (n < 100 || n > 999)
{
continue;
}
new convert(n);
}
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}