|
-
Dec 3rd, 2006, 07:54 AM
#1
Thread Starter
Fanatic Member
converting int to str
can you give me some simple code that will convert the integer to a string ...
coz i have a code but its too lonh like this
(example)
Code:
import java.io.*;
public class convert
{
public static void main(String args[])
{
BufferedReader woof=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 3 digits only: ");
int num=Integer.parseInt(woof.readLine);
if (num == 100)
{
System.out.println("one hundred")
}
if (num == 101)
{
System.out.println("one hundred and one")
}
and so on ...
i dnt know how to use the "String s = stringValueOg (val)" ... and array in java ....
so can anyone can give me a simple code of converting
-
Dec 4th, 2006, 06:19 PM
#2
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 5th, 2006, 07:34 AM
#3
Thread Starter
Fanatic Member
Re: converting int to str
can you give me an example using the "Integer.toString(value)"
-
Dec 5th, 2006, 08:03 AM
#4
Re: converting int to str
Code:
int i=1;
String s;
s=Integer.toString(i);
But that only parses (converts) Integer to String.
-
Dec 8th, 2006, 06:21 AM
#5
Thread Starter
Fanatic Member
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);
}
}
}
Last edited by Loraine; Dec 8th, 2006 at 06:25 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|