This is a program that converts number to a string. For example 224 is converted to two hundred and twenty four. This program is written using JAVA 1.6 and I used Eclipse IDE. In this program I used 3 classes. Main, Input and NumFinder. Used a simple logic. As a beginner I don't know any advanced methods and pleas comment about any errors and that will be welcome.
The Main class: /**
* @author Jayanga Kaushalya.
* This is a test program that converts number to it's literal.
* Eg: 224 to two hundred and twenty four. This program is written
* using Java 1.6
*
* jkaushalya@gmail.com
*/
public class Main {
public static void main(String[] args) {
new Input();
}
}
The Input class: import java.io.*;
/*
* For learning purpose all inputs are taken from command line.
* Using java buffered input.
*/
public class Input {
private String s = null;
public Input(){
NumFinder num = new NumFinder();
getUserInput();
num.print(s);
}
public void getUserInput(){
System.out.print("Enter number: ");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
}
catch (IOException e){
System.out.println(e);
}
}
}
The NumFinder class: /**
* The main logic happens here. The conversion process.
*/
public class NumFinder {
String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen"};
String[] tens = {"twenty", "thirty"};
String[] adjct = {"teen", "ty", "hundred", "thousand", "million", "billion", "trillion"};
int x = 0;
public void print(String s){
String number = null;//Main number
String cents = null;//Cents
double f_num = 0;//For get the fractional part.
long num = 0;
try {
f_num = Double.parseDouble(s);
num = (long) f_num;
}
catch (NumberFormatException e) { //Checking for invalid inputs such as alphabetical character.
System.err.println("Please enter a valid number!");
return;
}
if(num > 9.99999999999999E14){ // Only 999 trillions can be handled. Because i didn't write quadrilion function.
System.err.print("Number must bellow 1,000,000,000,000,000!!");
return;
}
int f_part = (int) Math.round(((f_num - num) * 100)); //Getting the fractional part.
if(f_part == 0)
cents = "zero";
else
cents = ten(f_part);
long tmp = num;
while(true){ //Dividing until 1 for getting number size.
tmp /= 10;
if(tmp < 1)
break;
x++;
}
// Sending the num to it's function according to size.
if(x == 0) number = one(num);
if(x == 1) number = ten(num);
if(x == 2) number = hundred(num);
if(x >= 3 && x <= 5) number = thousand(num);
if(x >= 6 && x <= 8) number = million(num);
if(x >= 9 && x <= 11) number = billion(num);
if( x >= 12 && x <= 14) number = trillion(num);
// Last part printing the string.
if(num == 0)
System.out.println("Zero");
else {
number = toCapital(number);//Will change the first letter to capital
System.out.println(number+" "+"and "+cents+" cents only.");
}
}
private String toCapital(String number){
/**
* First get the string in to char array. And then get first charcter's ascii value
* and add deduct 32 to from it to get the capital letter. And reverse the method.
*/
char[] word = null;
int ltr;
word = number.toCharArray();
ltr = (int) word[0];
ltr -= 32;
word[0] = (char) ltr;
number = String.copyValueOf(word);
return(number);
}
/**
* The same format is use along the entire program. So I didn't write comments
* for last part.
* The format is: for example if it's a thousand number 1234. Then 1 is handle by
* thousand method and 234 is passed to hundred method. Same in hundred method 2 is
* handled by hundred and 34 is passed to ten method.
*/
private String one(long num){
/**
* Handling numbers 1 - 9. Just printing array element of
* that number. Same format is use along.
*/
String number = null;
number = ones[(int) num];
return number;
}
private String ten(long num){ //Handling numbers 10 - 99
String number = null;
long index_one = num % 10;
long index_ten = num / 10;
if(num < 14) //If number below 10, it's passed to one method.
number = (one(num));
if(num < 20 && num > 13)
number = (one(index_one)+" "+adjct[0]);
if(num > 19 && num < 30)
number = (tens[0]+" "+one(index_one));
if(num > 29 && num < 40)
number = (tens[1]+" "+one(index_one));
else if(num > 39){
number = (one(index_ten)+adjct[1]+" "+one(index_one));
if(num > 49 && num < 60)
number = ("fifty"+" "+one(index_one));
}
return number;
}
private String hundred(long num){
String number = null;
if( num == 0 )
return " ";
long index_ten = num % 100; //The content that must pass to ten method.
long index_hundred = num / 100; //The content that this function must handle.
if (index_ten == 0)
number = (one(index_hundred)+" "+adjct[2]);
else
number = (one(index_hundred)+" "+adjct[2]+" "+ten(index_ten));
return number;
}
private String thousand(long num){
String number = null;
if (num == 0)
return " ";
long index_hundred = num % 1000;
long index_thosand = num / 1000;
if(index_thosand == 0)
return (number = hundred(num));
if(index_thosand < 100)
number = (ten(index_thosand)+" "+adjct[3]+" "+hundred(index_hundred));
if(index_thosand > 99)
number = (hundred(index_thosand)+" "+adjct[3]+" "+hundred(index_hundred));
return number;
}
private String million(long num){
String number = null;
long index_million = num / 1000000;
long index_thosand = num % 1000000;
if(index_million == 0)
return (number = thousand(num));
if( index_million < 100)
number = (ten(index_million)+" "+adjct[4]+" "+thousand(index_thosand));
if(index_million > 99)
number = (hundred(index_million)+" "+adjct[4]+" "+thousand(index_thosand));
return number;
}
private String billion(long num){
String number = null;
long index_billion = (long) (num / 1E9);
long index_million = (long) (num % 1E9);
if(index_billion == 0)
return (number = million(num));
if( index_billion < 100)
number = (ten(index_billion)+" "+adjct[5]+" "+million(index_million));
if(index_billion > 99)
number = (hundred(index_billion)+" "+adjct[5]+" "+million(index_million));
return number;
}
private String trillion(long num){
String number = null;
long index_trillion = (long) (num / 1E12);
long index_billion = (long) (num % 1E12);
if( index_trillion < 100)
number = (ten(index_trillion)+" "+adjct[6]+" "+billion(index_billion));
if(index_trillion > 99)
number = (hundred(index_trillion)+" "+adjct[6]+" "+billion(index_billion));
return number;
}
//If you want you can implement Quadrilion function according to this format.
}
0 comments:
Post a Comment