• We just launched and are currently in beta. Join us as we build and grow the community.

Number To Words in Java

jenkeyman

Digital Media Expert
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
62
Likes
96
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial will teach you how to create a program that will convert a number and will become words in java.

So, now let's start this tutorial!


1. Open JCreator or NetBeans and make a java program with a file name of NumberToWords.java.

2. Import the io library as we will need a user input.
  1. import

    java.io.*

    ;

3. Now, we will declare our global variables. We will have variables for the inputting number, ones,tens, elevens, hundreds, thousands, and the zero. We will have only a maximum input of 3000.
  1. static

    int

    n,thou,hun,tens,ones,zero,elevens;

We will create the BufferedReader class with variable in, and n as intger that will make as our input.
  1. BufferedReader

    in =

    new

    BufferedReader

    (

    new

    InputStreamReader

    (

    System

    .in

    )

    )

    ;

  2. System

    .out

    .println

    (

    "Enter Number: "

    )

    ;
  3. n =

    Integer

    .parseInt

    (

    in.readLine

    (

    )

    )

    ;

4. We will code first for inputting of the zero. We will just use the if and switch statement here. Have this code below:
  1. if

    (

    n==

    0

    )

    {

  2. switch

    (

    n)

    {
  3. case

    0

    :

    System

    .out

    .println

    (

    "Zero"

    )

    ;

    break

    ;

  4. }

5. Now, this code will be for the hundred and thousand when inputting 3 to 4 digits. We will use the '%' for the modulus as we have to get the zeroes and the remainder. Have this code below:
  1. if

    (

    (

    n>=

    100

    )

    &&

    (

    n<=

    3000

    )

    )

    {

  2. thou =

    n/

    1000

    ;
  3. n =

    n%

    1000;
  4. hun =

    n/

    100

    ;
  5. n =

    n%

    100;

  6. switch

    (

    thou)

    {

  7. case

    1

    :

    System

    .out

    .print

    (

    "One Thousand "

    )

    ;

    break

    ;
  8. case

    2

    :

    System

    .out

    .print

    (

    "Two Thousand "

    )

    ;

    break

    ;
  9. case

    3

    :

    System

    .out

    .print

    (

    "Three Thousand "

    )

    ;

    break

    ;
  10. }


  11. switch

    (

    hun)

    {
  12. case

    1

    :

    System

    .out

    .print

    (

    "One Hundred "

    )

    ;

    break

    ;
  13. case

    2

    :

    System

    .out

    .print

    (

    "Two Hundred "

    )

    ;

    break

    ;
  14. case

    3

    :

    System

    .out

    .print

    (

    "Three Hundred "

    )

    ;

    break

    ;
  15. case

    4

    :

    System

    .out

    .print

    (

    "Four Hundred "

    )

    ;

    break

    ;
  16. case

    5

    :

    System

    .out

    .print

    (

    "Five Hundred "

    )

    ;

    break

    ;
  17. case

    6

    :

    System

    .out

    .print

    (

    "Six Hundred "

    )

    ;

    break

    ;
  18. case

    7

    :

    System

    .out

    .print

    (

    "Seven Hundred "

    )

    ;

    break

    ;
  19. case

    8

    :

    System

    .out

    .print

    (

    "Eight Hundred "

    )

    ;

    break

    ;
  20. case

    9

    :

    System

    .out

    .print

    (

    "Nine Hundred "

    )

    ;

    break

    ;


  21. }
  22. }


6. For the eleven to nineteen, we will filter such code with the if condition followed by the switch statement.
  1. if

    (

    (

    n>

    10

    )

    &&

    (

    n<

    20

    )

    )

    {

  2. tens =

    n /

    10

    ;
  3. ones =

    n;
  4. elevens =

    n %

    10

    ;


  5. switch

    (

    elevens)

    {
  6. case

    1

    :

    System

    .out

    .print

    (

    "Eleven "

    )

    ;

    break

    ;
  7. case

    2

    :

    System

    .out

    .print

    (

    "Twelve "

    )

    ;

    break

    ;
  8. case

    3

    :

    System

    .out

    .print

    (

    "Thirteen "

    )

    ;

    break

    ;
  9. case

    4

    :

    System

    .out

    .print

    (

    "Fourteen "

    )

    ;

    break

    ;
  10. case

    5

    :

    System

    .out

    .print

    (

    "Fifteen "

    )

    ;

    break

    ;
  11. case

    6

    :

    System

    .out

    .print

    (

    "Sixteen "

    )

    ;

    break

    ;
  12. case

    7

    :

    System

    .out

    .print

    (

    "Seventeen "

    )

    ;

    break

    ;
  13. case

    8

    :

    System

    .out

    .print

    (

    "Eighteen "

    )

    ;

    break

    ;
  14. case

    9

    :

    System

    .out

    .print

    (

    "Nineteen "

    )

    ;

    break

    ;
  15. }
  16. }

7. Then have this code for the ones and tens.

  1. else

    {
  2. tens =

    n/

    10

    ;
  3. n =

    n%

    10;
  4. ones =

    n;

  5. switch

    (

    tens)

    {

  6. case

    1

    :

    System

    .out

    .print

    (

    "Ten "

    )

    ;

    break

    ;
  7. case

    2

    :

    System

    .out

    .print

    (

    "Twenty "

    )

    ;

    break

    ;
  8. case

    3

    :

    System

    .out

    .print

    (

    "Thirty "

    )

    ;

    break

    ;

  9. case

    4

    :

    System

    .out

    .print

    (

    "Fourty "

    )

    ;

    break

    ;
  10. case

    5

    :

    System

    .out

    .print

    (

    "Fifty "

    )

    ;

    break

    ;
  11. case

    6

    :

    System

    .out

    .print

    (

    "Sixty "

    )

    ;

    break

    ;
  12. case

    7

    :

    System

    .out

    .print

    (

    "Seventy "

    )

    ;

    break

    ;
  13. case

    8

    :

    System

    .out

    .print

    (

    "Eighty "

    )

    ;

    break

    ;
  14. case

    9

    :

    System

    .out

    .print

    (

    "Ninety "

    )

    ;

    break

    ;
  15. }
  16. switch

    (

    ones)

    {

  17. case

    1

    :

    System

    .out

    .print

    (

    "One "

    )

    ;

    break

    ;
  18. case

    2

    :

    System

    .out

    .print

    (

    "Two "

    )

    ;

    break

    ;
  19. case

    3

    :

    System

    .out

    .print

    (

    "Three "

    )

    ;

    break

    ;

  20. case

    4

    :

    System

    .out

    .print

    (

    "Four "

    )

    ;

    break

    ;
  21. case

    5

    :

    System

    .out

    .print

    (

    "Five "

    )

    ;

    break

    ;
  22. case

    6

    :

    System

    .out

    .print

    (

    "Six "

    )

    ;

    break

    ;
  23. case

    7

    :

    System

    .out

    .print

    (

    "Seven "

    )

    ;

    break

    ;
  24. case

    8

    :

    System

    .out

    .print

    (

    "Eight "

    )

    ;

    break

    ;
  25. case

    9

    :

    System

    .out

    .print

    (

    "Nine "

    )

    ;

    break

    ;

  26. }

  27. }

8. Remember that our conversion will have only its maximum number of 3000, so we must filter it. Have this code:
  1. if

    (

    n>

    3000

    )

    {
  2. System

    .out

    .print

    (

    "INVALID INPUT"

    )

    ;
  3. }


Output:

numbertowords.png


Here's the full code of this tutorial:

  1. import

    java.io.*

    ;
  2. public

    class

    NumberToWords {

  3. static

    int

    n,thou,hun,tens,ones,zero,elevens;

  4. public

    static

    void

    main(

    String

    args[

    ]

    )

    throws

    IOException

    {


  5. BufferedReader

    in =

    new

    BufferedReader

    (

    new

    InputStreamReader

    (

    System

    .in

    )

    )

    ;

  6. System

    .out

    .println

    (

    "Enter Number: "

    )

    ;
  7. n =

    Integer

    .parseInt

    (

    in.readLine

    (

    )

    )

    ;

  8. if

    (

    n==

    0

    )

    {

  9. switch

    (

    n)

    {
  10. case

    0

    :

    System

    .out

    .println

    (

    "Zero"

    )

    ;

    break

    ;

  11. }
  12. }

  13. if

    (

    (

    n>=

    100

    )

    &&

    (

    n<=

    3000

    )

    )

    {

  14. thou =

    n/

    1000

    ;
  15. n =

    n%

    1000;
  16. hun =

    n/

    100

    ;
  17. n =

    n%

    100;

  18. switch

    (

    thou)

    {

  19. case

    1

    :

    System

    .out

    .print

    (

    "One Thousand "

    )

    ;

    break

    ;
  20. case

    2

    :

    System

    .out

    .print

    (

    "Two Thousand "

    )

    ;

    break

    ;
  21. case

    3

    :

    System

    .out

    .print

    (

    "Three Thousand "

    )

    ;

    break

    ;
  22. }


  23. switch

    (

    hun)

    {
  24. case

    1

    :

    System

    .out

    .print

    (

    "One Hundred "

    )

    ;

    break

    ;
  25. case

    2

    :

    System

    .out

    .print

    (

    "Two Hundred "

    )

    ;

    break

    ;
  26. case

    3

    :

    System

    .out

    .print

    (

    "Three Hundred "

    )

    ;

    break

    ;
  27. case

    4

    :

    System

    .out

    .print

    (

    "Four Hundred "

    )

    ;

    break

    ;
  28. case

    5

    :

    System

    .out

    .print

    (

    "Five Hundred "

    )

    ;

    break

    ;
  29. case

    6

    :

    System

    .out

    .print

    (

    "Six Hundred "

    )

    ;

    break

    ;
  30. case

    7

    :

    System

    .out

    .print

    (

    "Seven Hundred "

    )

    ;

    break

    ;
  31. case

    8

    :

    System

    .out

    .print

    (

    "Eight Hundred "

    )

    ;

    break

    ;
  32. case

    9

    :

    System

    .out

    .print

    (

    "Nine Hundred "

    )

    ;

    break

    ;


  33. }
  34. }


  35. if

    (

    (

    n>

    10

    )

    &&

    (

    n<

    20

    )

    )

    {

  36. tens =

    n /

    10

    ;
  37. ones =

    n;
  38. elevens =

    n %

    10

    ;


  39. switch

    (

    elevens)

    {
  40. case

    1

    :

    System

    .out

    .print

    (

    "Eleven "

    )

    ;

    break

    ;
  41. case

    2

    :

    System

    .out

    .print

    (

    "Twelve "

    )

    ;

    break

    ;
  42. case

    3

    :

    System

    .out

    .print

    (

    "Thirteen "

    )

    ;

    break

    ;
  43. case

    4

    :

    System

    .out

    .print

    (

    "Fourteen "

    )

    ;

    break

    ;
  44. case

    5

    :

    System

    .out

    .print

    (

    "Fifteen "

    )

    ;

    break

    ;
  45. case

    6

    :

    System

    .out

    .print

    (

    "Sixteen "

    )

    ;

    break

    ;
  46. case

    7

    :

    System

    .out

    .print

    (

    "Seventeen "

    )

    ;

    break

    ;
  47. case

    8

    :

    System

    .out

    .print

    (

    "Eighteen "

    )

    ;

    break

    ;
  48. case

    9

    :

    System

    .out

    .print

    (

    "Nineteen "

    )

    ;

    break

    ;
  49. }
  50. }
  51. if

    (

    n>

    3000

    )

    {
  52. System

    .out

    .print

    (

    "INVALID INPUT"

    )

    ;
  53. }

  54. else

    {
  55. tens =

    n/

    10

    ;
  56. n =

    n%

    10;
  57. ones =

    n;

  58. switch

    (

    tens)

    {

  59. case

    1

    :

    System

    .out

    .print

    (

    "Ten "

    )

    ;

    break

    ;
  60. case

    2

    :

    System

    .out

    .print

    (

    "Twenty "

    )

    ;

    break

    ;
  61. case

    3

    :

    System

    .out

    .print

    (

    "Thirty "

    )

    ;

    break

    ;

  62. case

    4

    :

    System

    .out

    .print

    (

    "Fourty "

    )

    ;

    break

    ;
  63. case

    5

    :

    System

    .out

    .print

    (

    "Fifty "

    )

    ;

    break

    ;
  64. case

    6

    :

    System

    .out

    .print

    (

    "Sixty "

    )

    ;

    break

    ;
  65. case

    7

    :

    System

    .out

    .print

    (

    "Seventy "

    )

    ;

    break

    ;
  66. case

    8

    :

    System

    .out

    .print

    (

    "Eighty "

    )

    ;

    break

    ;
  67. case

    9

    :

    System

    .out

    .print

    (

    "Ninety "

    )

    ;

    break

    ;
  68. }
  69. switch

    (

    ones)

    {

  70. case

    1

    :

    System

    .out

    .print

    (

    "One "

    )

    ;

    break

    ;
  71. case

    2

    :

    System

    .out

    .print

    (

    "Two "

    )

    ;

    break

    ;
  72. case

    3

    :

    System

    .out

    .print

    (

    "Three "

    )

    ;

    break

    ;

  73. case

    4

    :

    System

    .out

    .print

    (

    "Four "

    )

    ;

    break

    ;
  74. case

    5

    :

    System

    .out

    .print

    (

    "Five "

    )

    ;

    break

    ;
  75. case

    6

    :

    System

    .out

    .print

    (

    "Six "

    )

    ;

    break

    ;
  76. case

    7

    :

    System

    .out

    .print

    (

    "Seven "

    )

    ;

    break

    ;
  77. case

    8

    :

    System

    .out

    .print

    (

    "Eight "

    )

    ;

    break

    ;
  78. case

    9

    :

    System

    .out

    .print

    (

    "Nine "

    )

    ;

    break

    ;

  79. }

  80. }

  81. }
  82. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top