| | |
how to convert digit(number) into word
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
you have to "hard code it" with use of array for example
first return will be string zero and second one will be three
Java Syntax (Toggle Plain Text)
String[] myArray={"zero", "one", two", "three"}; int num = 0; System.out.println(myArray[num]); num = 3; System.out.println(myArray[num]);
first return will be string zero and second one will be three
Last edited by peter_budo; Jan 15th, 2007 at 8:30 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
which won't provide a correct solution in almost any situation 
You'll need a lot more logic than that, but it could be used as a starting point.
Instead of an array though you should really consider using an enum.

You'll need a lot more logic than that, but it could be used as a starting point.
Instead of an array though you should really consider using an enum.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
This is going to be harder than you think due to the fact that numbers are represented differently depending on the position in the number the specific digit is in. For example, if a 5 is in the one's slot, it is represented as "five", whereas in the ten's spot it is fifty-[ones digit number(unless zero)], then back to "five" for the hundreds and so on.
In a nutshell, you would need a fair amount of logic as jwenting stated, just figured I would elaborate some more.
Regards,
Tyler S. Breton
In a nutshell, you would need a fair amount of logic as jwenting stated, just figured I would elaborate some more.
Regards,
Tyler S. Breton
Western New England College '08
Computer Science
Programming Lang's:
C, C#, Java, Lisp, MASM, Visual Basic 6
Computer Science
Programming Lang's:
C, C#, Java, Lisp, MASM, Visual Basic 6
I would approach this making an ArrayList of strings instead of a list (tad more versitile, but list of Strings will do as well) and then create a string variable that you can do x = x + number.toString();
Perhaps some if statements will solve the other have of the problem
if (number ==1) then list.get(n-1);
Perhaps some if statements will solve the other have of the problem

if (number ==1) then list.get(n-1);
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
HIYA GUYS
VERY BASIC WAY OF DOING IT AND ONLY HANDLES UP TO NUMBER 999 ALTHOUGH CAN BE ALTERED PRETTY EASILY.
THIS CODE DOES NOT USE CONFUSING ARRAYS WHICH WOULD CUT DOWN MY CODE A LOT BUT THIS CODE IS THE FULL WHACK AND THE WAY THEY WOULD TEACH SOMEONE AT UNIVERSITY AT BASIC LEVEL.
COPY AND PASTE INTO JAVA EDITOR...
OBVIOUSLY A GOOD WAY TO UNDERSTAND MY CODE PROPERLY OR ANY CODE IS TO USE THE DEBUG OPTION IN UR JAVA EDITOR TO GO THROUGH THE STEPS ONE BY ONE FOR ANY GIVEN NUMBER
THANKS BYE...
--------------------------------
VERY BASIC WAY OF DOING IT AND ONLY HANDLES UP TO NUMBER 999 ALTHOUGH CAN BE ALTERED PRETTY EASILY.
THIS CODE DOES NOT USE CONFUSING ARRAYS WHICH WOULD CUT DOWN MY CODE A LOT BUT THIS CODE IS THE FULL WHACK AND THE WAY THEY WOULD TEACH SOMEONE AT UNIVERSITY AT BASIC LEVEL.
COPY AND PASTE INTO JAVA EDITOR...
OBVIOUSLY A GOOD WAY TO UNDERSTAND MY CODE PROPERLY OR ANY CODE IS TO USE THE DEBUG OPTION IN UR JAVA EDITOR TO GO THROUGH THE STEPS ONE BY ONE FOR ANY GIVEN NUMBER
THANKS BYE...
--------------------------------
Java Syntax (Toggle Plain Text)
// Program to convert integers into words, Ex5.5 package num2word; import java.util.Scanner; public class Main { // 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; } } public static void main(String[] args) { Scanner input = new Scanner(System.in); // Requesting User input System.out.print("Enter a number between 0 and 999: "); int number = input.nextInt(); // input validation while (number < 0 || number > 999) { System.out.println("Input Too Large, "); System.out.print("Enter a number between 0 and 999: "); number = input.nextInt(); } // Below we start to determine what the input was by // categorising it using boolean // 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 numbers) if (number > 10 && number < 20) { // (Special numbers) special(number); // print number } // original number (0-9) if (number < 10) { // finding the units (one, two) units(number); // print number } } }
![]() |
Similar Threads
- C++:three-digit number all odd, eve, or both (C++)
- How do i print int num1; a 5 digit number like 1 2 3 4 5 (Java)
Other Threads in the Java Forum
- Previous Thread: convert nested for loop into nested while loop
- Next Thread: Question about order of retrieved elements in XML
| Thread Tools | Search this Thread |
Tag cloud for Java
@param android animated api appinventor apple applet application arguments array arrays automation binary bluetooth c++ chat chatprogramusingobjects chooser class classes client code codesnippet compiler component coordinates database doctype draw eclipse editor error event exception file fractal freeze game givemetehcodez graphics gui guidancer health helpwithhomework html ide image input int integer j2me java javaprojects jmf jni jpanel jtable julia linux list login loop map method methods mobile netbeans newbie nonstatic number oracle page plazmic print problem program programming project recursion scanner screen sell server set sharepoint size sms socket sort sourcelabs sql string swing system test testautomation threads time tree windows






