Shorter code: Spell Out Numbers
Hi I'm making a code to spell out a number from 0 to 199. Can you check this out:
Code:
import javax.swing.*;
public class Say {
static String ones[] = {" ","one","two","three","four","five","six","seven","eight","nine"};
static String teen[] = {
"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"
};
static String tens[] = {
" ","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
public static void main(String args[]) {
int num =Integer.parseInt(JOptionPane.showInputDialog("Enter a number from 0-199"));
int hundred = num/100; //=1.28
int rem = num % 100;
int ten = rem / 10;
int one =rem % 10;
if (hundred == 0) {
System.out.println(tens[ten] + " " + ones[one]);
}
else if(rem == 0) {
System.out.println(ones[hundred] + " hundred");
}
else{
if ((rem >=10) && (rem <= 19)) {
System.out.println(ones[hundred] + " hundred" + " " + teen[one]);
} else {
System.out.println(ones[hundred] + " hundred" + " " + tens[ten] + " " + ones[one]);}
}
}
}
Is there any possible way to make it shorter? Because my plan is to read numbers until a million.
Are there any mistake in the code or some other ways to make it cleaner?
Thank you.