Hello,
I already have the program ( convert number to words but only 0-999 ). I need it to be ( 0-9999 ).
can you help me?
Thanks
First off, split the cases up smarter. eg pseudocode: -
Currently you have:
-
if (thousands){
-
print thousands
-
print hundreds
-
print special
-
print tens
-
print ones
-
} else {
-
print hundreds
-
print special
-
print tens
-
print ones
-
}
-
-
Whereas, to simplify the code, you really should have:
-
If (thousands?)
-
Print thousands
-
if (hundreds?)
-
Print hundreds
-
if (special?){
-
print special
-
} else {
-
print tens
-
print ones
-
}
ALSO!! You could simply add a case 0:
case 0: // output nothing
break;
2. the way you're getting your tens digit is off.
int tens = x / 10;
should be
int tens = x/10 % 10;
You can do this for all of your number parts. eg
thousands = number / 1000 % 10;
hundreds = number / 100 % 10;
tens = number / 10 % 10;
units = number % 10;
4 19761
Here's the updated program: -
package num2word;
-
-
import java.util.Scanner;
-
-
public class NumbertoWords {
-
-
// units Method
-
public static void units(int n) {
-
-
switch (n) {
-
case 1:
-
System.out.print("one ");
-
break;
-
case 2:
-
System.out.print("two ");
-
break;
-
case 3:
-
System.out.print("three ");
-
break;
-
case 4:
-
System.out.print("four ");
-
break;
-
case 5:
-
System.out.print("five ");
-
break;
-
case 6:
-
System.out.print("six ");
-
break;
-
case 7:
-
System.out.print("seven ");
-
break;
-
case 8:
-
System.out.print("eight ");
-
break;
-
case 9:
-
System.out.print("nine ");
-
break;
-
}
-
}
-
-
// special case Method
-
public static void special(int n) {
-
-
switch (n) {
-
case 11:
-
System.out.print(" eleven");
-
break;
-
case 12:
-
System.out.print(" twelve");
-
break;
-
case 13:
-
System.out.print(" thirteen");
-
break;
-
case 14:
-
System.out.print(" fourteen");
-
break;
-
case 15:
-
System.out.print(" fifteen");
-
break;
-
case 16:
-
System.out.print(" sixteen");
-
break;
-
case 17:
-
System.out.print(" seventeen");
-
break;
-
case 18:
-
System.out.print(" eighteen");
-
break;
-
case 19:
-
System.out.print(" nineteen");
-
break;
-
}
-
}
-
-
// tens Method
-
public static void tens(int n) {
-
-
switch (n) {
-
case 1:
-
System.out.print(" ten ");
-
case 2:
-
System.out.print(" twenty ");
-
break;
-
case 3:
-
System.out.print(" thirty ");
-
break;
-
case 4:
-
System.out.print(" forty ");
-
break;
-
case 5:
-
System.out.print(" fifty ");
-
break;
-
case 6:
-
System.out.print(" sixty ");
-
break;
-
case 7:
-
System.out.print(" seventy ");
-
break;
-
case 8:
-
System.out.print(" eighty ");
-
break;
-
case 9:
-
System.out.print(" ninety ");
-
break;
-
}
-
}
-
-
// hundreds Method
-
public static void hundreds(int n) {
-
-
switch (n) {
-
case 1:
-
System.out.print("one hundred ");
-
break;
-
case 2:
-
System.out.print("two hundred ");
-
break;
-
case 3:
-
System.out.print("three hundred ");
-
break;
-
case 4:
-
System.out.print("four hundred ");
-
break;
-
case 5:
-
System.out.print("five hundred ");
-
break;
-
case 6:
-
System.out.print("six hundred ");
-
break;
-
case 7:
-
System.out.print("seven hundred ");
-
break;
-
case 8:
-
System.out.print("eight hundred ");
-
break;
-
case 9:
-
System.out.print("nine hundred ");
-
break;
-
}
-
}
-
// hundreds Method
-
public static void thousands(int n) {
-
-
switch (n) {
-
case 1:
-
System.out.print("one thousand ");
-
break;
-
case 2:
-
System.out.print("two thousand ");
-
break;
-
case 3:
-
System.out.print("three thousand ");
-
break;
-
case 4:
-
System.out.print("four thousand ");
-
break;
-
case 5:
-
System.out.print("five thousand ");
-
break;
-
case 6:
-
System.out.print("six thousand ");
-
break;
-
case 7:
-
System.out.print("seven thousand ");
-
break;
-
case 8:
-
System.out.print("eight thousand ");
-
break;
-
case 9:
-
System.out.print("nine thousand ");
-
break;
-
}
-
}
-
public static void main(String[] args) {
-
Scanner input = new Scanner(System.in);
-
-
// Requesting User input
-
System.out.print("Enter an integer number[0 to 9999]::>> ");
-
int number = input.nextInt();
-
-
// input validation
-
while (number < 0 || number > 9999) {
-
System.out.println("Input Too Large, ");
-
System.out.print("Enter an integer number[0 to 9999]::>> ");
-
number = input.nextInt();
-
}
-
-
// Below we start <strong class="highlight">to</strong> determine what the input was by
-
// categorising it using boolean
-
-
// thousands (1000-9999)
-
if (number > 999 && number < 10000) {
-
int t = number / 1000; //find the hundreds only (one, two hundred)
-
thousands(t); //print number
-
-
-
// first part of number found & printed (hundreds)
-
// remamining 2 digits must be found ie tens & units
-
-
// test if they are a special number
-
int x = 0; // initialized variable x for calculations
-
x = number % 1000; // find remainder of hundreds (ie x54)
-
if (x > 10 && x < 20) { // ie is 54 a special number?
-
special(x); // print number
-
}
-
// if not special number -> split up into tens & units
-
if (x > 0 && x < 1000) {
-
-
int hundreds = x / 100; // finding the hundreds
-
hundreds(hundreds); // print number
-
-
int tens = x / 10; // finding the tens (forty, fifty)
-
tens(tens); // print number
-
-
int units = x % 10; // finding the units (one, two)
-
units(units); // print number
-
}
-
}
-
// original number (100-999)
-
if (number > 99 && number < 1000) {
-
int h = number / 100; //find the hundreds only (one, two hundred)
-
hundreds(h); //print number
-
-
-
// first part of number found & printed (hundreds)
-
// remamining 2 digits must be found ie tens & units
-
-
// test if they are a special number
-
int x = 0; // initialized variable x for calculations
-
x = number % 100; // find remainder of hundreds (ie x54)
-
if (x > 10 && x < 20) { // ie is 54 a special number?
-
special(x); // print number
-
}
-
// if not special number -> split up into tens & units
-
if (x > 0 && x < 100) {
-
-
int tens = x / 10; // finding the tens (forty, fifty)
-
tens(tens); // print number
-
-
int units = x % 10; // finding the units (one, two)
-
units(units); // print number
-
}
-
}
-
-
// original number (20-99)
-
if (number > 19 && number < 100) {
-
-
int t = number / 10; // finding the tens (forty, fifty)
-
tens(t); // print number
-
-
int u = number % 10; // finding the units (one, two)
-
units(u); // print number
-
}
-
-
// original number (11-19) (Special <strong class="highlight">numbers</strong>)
-
if (number > 10 && number < 20) { // (Special <strong class="highlight">numbers</strong>)
-
special(number); // print number
-
}
-
-
// original number (0-9)
-
if (number < 10) { // finding the units (one, two)
-
units(number); // print number
-
}
-
}
-
}
The output is: -
Enter an integer number[0 to 999]::>> 5670
-
five thousand six hundred
-
Process completed.
-
I dont know where the problem is.
help please?
thanks!
I guess it would be simpler if you convert the input value into a string. access the element of that string and get the representation....
like this one? -
public class NumberToWords {
-
-
private static final String[] ONES = {
-
"Zero", "One", "Two", "Three", "Four", "Five",
-
"Six", "Seven", "Eight", "Nine" };
-
private static final String[] TEENS = {
-
"Ten", "Eleven", "Twelve", "Thirteen", null, "Fifteen",
-
null, null, "Eighteen", null };
-
private static final String[] TENS = {
-
null, null, "Twenty", "Thirty", "Forty", "Fifty",
-
"Sixty", "Seventy", "Eighty", "Ninety" };
-
-
public static String numberToWords(int number) {
-
if (number<10) {
-
return ONES[number];
-
} else if (number<20) {
-
int n = number - 10;
-
String words = TEENS[n];
-
return words==null ? ONES[n]+"teen" : TEENS[n];
-
} else {
-
int n = number % 10;
-
return TENS[number/10] +
-
(n==0 ? "" : (" " + numberToWords(n)));
-
}
-
}
-
-
public static void main(String[] args) {
-
for (int i=0; i<100; i++) {
-
System.out.println(i+" "+numberToWords(i));
-
}
-
}
-
}
-
First off, split the cases up smarter. eg pseudocode: -
Currently you have:
-
if (thousands){
-
print thousands
-
print hundreds
-
print special
-
print tens
-
print ones
-
} else {
-
print hundreds
-
print special
-
print tens
-
print ones
-
}
-
-
Whereas, to simplify the code, you really should have:
-
If (thousands?)
-
Print thousands
-
if (hundreds?)
-
Print hundreds
-
if (special?){
-
print special
-
} else {
-
print tens
-
print ones
-
}
ALSO!! You could simply add a case 0:
case 0: // output nothing
break;
2. the way you're getting your tens digit is off.
int tens = x / 10;
should be
int tens = x/10 % 10;
You can do this for all of your number parts. eg
thousands = number / 1000 % 10;
hundreds = number / 100 % 10;
tens = number / 10 % 10;
units = number % 10;
Sign in to post your reply or Sign up for a free account.
Similar topics
by: david |
last post by:
HI!
Im trying to make "HTML form" into automatic.
1. If I get 18 numbers like: A B C D E F . . . .
2. How can I put those 18 numbers automatically into 6 numbers format
like:
A B C D E F
|
by: Douglas |
last post by:
Gday,
How would I format a number so that:
TheValue = 32500
Displays in the TextBox as: $32,500.00
|
by: vanvee |
last post by:
Hi
I have an application for my company's HR department where we store
resumes for candidates we receive. I have an application that uses
VB.Net and ADO.Net and data bindings (through code) to...
|
by: oviam packirisamy |
last post by:
how to convert number to string in sqlserver
eg:- 10 -> ten display
*** Sent via Developersdex http://www.developersdex.com ***
|
by: Ntl News Group |
last post by:
I have a text box that contains the number 1234, I want the value form
the text box to display in a second text box so I used:
=
The problems is that this displays 1234 and I want it to display...
|
by: mustang07 |
last post by:
I need this program to find all occurences of a name and display them.
If I enter Palmer, I need it to display all the Palmers in the list.
Can anyone help me Please. Thank You!!
#include...
|
by: Gonzchris |
last post by:
What would be the easiest way of going about converting a 3 digit hexadecimal number to decimal, I think that a switch statement is out of the question due to multiple digits. Would it be easiest...
|
by: malmus |
last post by:
<%# DataBinder.Eval(Container.DataItem, "ShortText")%>
I have the code above in C# and i want to display only the first 100 words. How can i do that??
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |