HI im a student and our professor asked us to convert a number into word:

example:
input= "1"
output="one"

i need to have numbers from 1-10,001
so my program will end up very long if i only use if else statement i know that there are other methods other than this please help me thank you

Recommended Answers

All 70 Replies

create two arrays
one containing 1, 2, 3, ... , 19
and one containing one, two, three, ..., nineteen
then create the same kind of arrays, that 'll deal with the decimals:
20,30
twenty, thirty, ...
do the same for hundreds and thousands
and use indexes to find the correct matches using the indices of these arrays.

create two arrays
one containing 1, 2, 3, ... , 19
and one containing one, two, three, ..., nineteen
then create the same kind of arrays, that 'll deal with the decimals:
20,30
twenty, thirty, ...
do the same for hundreds and thousands
and use indexes to find the correct matches using the indices of these arrays.

sir thank u but..its a bnit hard for me to understand because literally i am new to java. im sorry sir but can u elaborate it a little more? thank you

Are you at least familiar with arrays?

Here's a link from the API
Arrays

according to the forum-rules: no.
you first have to try by yourself, and come back with any logical or syntax errors you get. we're vollunteering to help you overcome your problems, but we won't write your code from scratch.

Are you at least familiar with arrays?

Here's a link from the API
Arrays

thanks ..but even if i use array does that mean that i have to do these like 10,000 times? is there no other way? because ill be doing a program that will output word till "ten thousand and one"

thank you for being patient to me :)

no
about
19 + 7 would suffice for the numbers, and a few extra for the words linked to them

according to the forum-rules: no.
you first have to try by yourself, and come back with any logical or syntax errors you get. we're vollunteering to help you overcome your problems, but we won't write your code from scratch.

thanks sir i understand. I also want to develop my self into a good programmer. Sir does that mean that even if i use arrays i will still type it until i reach 10,000?
because our teacher told us to use if else so this is what i did

String words;
	     int num = Integer.parseInt(JOptionPane.showInputDialog("Enter Number"));
	      		       
	     if (num == 1) {
			            words = "one";
			        } else if (num == 2) {
			            words = "two";
			        } else if (num == 3) {
			            words = "three";
			        } else if (num == 4) {
			            words = "four";
			        } else if (num == 5) {
			            words = "five";
			        } else if (num == 6) {
			            words = "six";
			        } else if (num == 7) {
			            words = "seven";
			        } 
			        
			        else {
			            words = "unidentified";
			        }
			        JOptionPane.showMessageDialog(null, "Translation in words = " + words);
	}

}

thanks ..but even if i use array does that mean that i have to do these like 10,000 times? is there no other way? because ill be doing a program that will output word till "ten thousand and one"

thank you for being patient to me :)

not really the values in the array(words or numbers that will be used)... by my guess should be less than 100

EDIT:

no
about
19 + 7 would suffice for the numbers, and a few extra for the words linked to them

I was right

thanks sir i understand. I also want to develop my self into a good programmer. Sir does that mean that even if i use arrays i will still type it until i reach 10,000?
because our teacher told us to use if else so this is what i did

String words;
int num = Integer.parseInt(JOptionPane.showInputDialog("Enter Number"));

if (num == 1) {
words = "one";
} else if (num == 2) {
words = "two";
} else if (num == 3) {
words = "three";
} else if (num == 4) {
words = "four";
} else if (num == 5) {
words = "five";
} else if (num == 6) {
words = "six";
} else if (num == 7) {
words = "seven";
}

else {
words = "unidentified";
}
JOptionPane.showMessageDialog(null, "Translation in words = " + words);
}

}

as I said: try this:

int[] arrNumbers = {1,2,3,4};
String[] arrWords = {"one","two","three","four"};

and use the index of the number you've entered in the first array, to find the word in the second array

to Edit myself:
was a bit tired when I wrote my last posts,
for the numbers, you only need one array of 19 elements that you reuse :)

well, you may want to check out this link. normally, I would direct you to the array tutorials on the Oracle site, but I seem to have trouble loading the page.
On this page, you'll see how to use arrays and how to work with the index.

well, you may want to check out this link. normally, I would direct you to the array tutorials on the Oracle site, but I seem to have trouble loading the page.
On this page, you'll see how to use arrays and how to work with the index.

my classmate told me that he used "dispense" inside it he has the "if else statement may i know what a "dispense" is?

it's not a java technique AFAIK, but, in the approach I would take, you would have to divide the amount in
thousands
hundreds
...
I guess you could call that 'to dispense the amount', in a certain way, allthough english is not my first language, so I'm not entirely sure :)

commented: Yes: Finally an answer that suggests a viable general approach! +7

@Ms New to Java I do recommend that you stop sending personal messages(PM) to forum members. It is unwelcome practice.
All the help you got so far is good, but it is up to you to use it. This is NOT 24/7 coding forum for lazy people.

it's not a java technique AFAIK, but, in the approach I would take, you would have to divide the amount in
thousands
hundreds
...
I guess you could call that 'to dispense the amount', in a certain way, allthough english is not my first language, so I'm not entirely sure :)

me to English is not my first language that is the other reason why i cant fully understand some of the other terms that u are talking about.:)

@Ms New to Java I do recommend that you stop sending personal messages(PM) to forum members. It is unwelcome practice.
All the help you got so far is good, but it is up to you to use it. This is NOT 24/7 coding forum for lazy people.

sorry sir i will not do it anymore

just for the hell of it (and because I'm bored off my socks) I just wrote that application you need to make.

now, I'm not just going to give you the code, since that would be cheating, and breaking the forum rules both, but just to get you started:

this are the only three arrays you 'll need to complete your assignment:

static int[] arrNumbers =  {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    static String[] arrWords1= {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eightteen","nineteen"};
    static String[] arrWords2= {"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

int[] arrNumbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};

ie arrNumbers == i+1

really?

ie arrNumbers == i+1

really?

off course you can do without, but you'll still need to test whether or not the element is within range.
I just assumed that someone who is not familiar with the use of arrays or indexes might find it easy to follow if there's a direct (visible) connection between the index and the value.

@stultuske you better off if you include zero ;) , also there is no need for first integer array

off course you can do without, but you'll still need to test whether or not the element is within range.

... which you don't need to do if you are going to access the array???
Seriously, I would be interested to see your code where this array is better than just (i+1).

@stultuske you better off if you include zero ;) , also there is no need for first integer array

since I doubt you need to print "zerothousand zerohundred fourteen" when 14 is the input, I deliberately did NOT put the 0 in that array :)
and as I already said before, answering JamesCherrill, indeed, you don't need the first array, I'm aware of that, but IMHO it might be easier for the OP to see the link, and if not, all that happened is that I wrote some silly code :)

OK, fair enough. I was only pursuing it in case you had had some cunning idea that I could steal :)

OK, fair enough. I was only pursuing it in case you had had some cunning idea that I could steal :)

I'm not Baldrick, I'm afraid :)
(and if you don't know what I mean by that, I'm referring to the Black Adder character)

after doing this what should i do next?

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] anArray = { 
			    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,161,7,18,19
			};
	
		String[] arrWords = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
	   
		int[] annArray = { 20,30,40,50,60,70,80,90};
		String[] tenths= {"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
		int[] hundredsnum = { 100,200,300,400,500,600,700,800,900};
		String [] hundredsword = {"one hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred","eight hundred","nine hundred"};
		int [] thounum = {1000,2000,3000,4000,5000,6000,7000,8000,9000,10000};
		
		
	
	}
	

}

am i getting close?

you don't need the annArray or hundredsNum, ...
just read your number
divide the original number by thousand
and print this number following by the word 'thousand'
you subtract this number (*1000, off course) from your original number and do the same for hundreds, ..

after that, you check how to link the words to the numbers, but as you can see above, all you need for words is one to nineteen and ten to ninety, since above that, it is one hundred

you don't need the annArray or hundredsNum, ...
just read your number
divide the original number by thousand
and print this number following by the word 'thousand'
you subtract this number (*1000, off course) from your original number and do the same for hundreds, ..

after that, you check how to link the words to the numbers, but as you can see above, all you need for words is one to nineteen and ten to ninety, since above that, it is one hundred

whta is that original number? i dont think that i have original number because i will be the one who will INPUT a number ... :(!

and, as soon as you inputted it, that value is the original number I was talking about :)

and, as soon as you inputted it, that value is the original number I was talking about :)

ok i understand ..what i did not understand is this part:

and print this number following by the word 'thousand'
you subtract this number (*1000, off course) from your original number and do the same for hundreds, ..

after that, you check how to link the words to the numbers, but as you can see above, all you need for words is one to nineteen and ten to ninety, since above that, it is one hundred

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.