hi can you please improve my code.. thanks hope you can all help me...

here's my codes:

import java.util.Scanner;

public class NumberToWord
{

    public static final String[] DIGITS = {"one", "two", "three", "four", "five",
    "six", "seven", "eight", "nine"};
    public static final String[] TENS = {null, "twenty", "thirty", "forty", "fifty",
    "sixty", "seventy", "eighty", "ninety"};
    public static final String[] TEENS = {"ten", "eleven", "twelve", "thirteen", "fourteen",
    "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

// use for every grouping of 10^3 - thousands, millions, billions, etc.
    public static String wordifyNumber(int number)
    {
        StringBuilder sb = new StringBuilder();

        int jbd = number / 100;
        if (jbd > 0)
        { // something in hundreds column
            sb.append(DIGITS[jbd - 1] + " hundred");
        }
        jbd = number % 100;
        int tens = jbd / 10;
        if (tens > 0) { // something in tens column
            if (sb.length() > 0)
            {
                sb.append(" ");
            }
            if (tens > 1)
            {
                sb.append(TENS[tens - 1]);
            } else
            {
                sb.append(TEENS[jbd - 10]);
                // TEENS accounts for tens + digit; done!
                number = 0;
            }
        }
        jbd = number % 10;
        if (jbd > 0)
        { // something in digits column
            if (sb.length() > 0)
            {
                if (tens >= 2)
                { // twenty-, thirty-, etc.
                    sb.append("-");
                }
                else
                {
                    sb.append(" ");
                }
            }
            sb.append(DIGITS[jbd - 1]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Enter a number (or -1 to quit): ");
            int number = scanner.nextInt();
            if (number == -1) {
                break;
            } else if (number == 0) {
                System.out.println("Number = zero");
            } else {
                System.out.print("Number = ");
                if (number > 999) {
                    System.out.print(
                        wordifyNumber(number / 1000) + " thousand");
                    number = number % 1000;
                    System.out.print(number > 99 ? ", " : " ");
                }
                System.out.println(
                    wordifyNumber(number));
            }
        }
        System.out.println("Bye.");
    }
}

Recommended Answers

All 13 Replies

what you actually want to do?

the program you written has no errors look like and it is correct.

then what you want..

add decimal numbers

please.. if i convert 9999.99 it is error.. and if i enter "x" the program will be ended

the scanner reads inputs as a string ..can you try to give input as a string like "9999.99" instead of 9999.99

it doesn't work.i already tried that one.. can you revise my code?? please help me

sorry i don't have time to spend with you..

you get input as string and use split or stringtokenizer to get decimal within quotes..

for eg:

String str="9999.99";
      String str2[]=str.split("\"");
      int dec=Integer.parseInt(str2[0]);

Okay, you want it to handle decimals. You can read a decimal, right? You can take the string "123.456" and parse it into a double?

Okay, you could read that in a couple of different ways, but the easiest is "one hundred twenty three point four five six."

So take your double, and break it up into the integer part and the fractional part. The integer part you've already done, so you just have to figure out the fractional part.

so i must put put also a double? okay thanks

You know, I think Mustafa's idea is a better one. Get the input as a String, and split the string on ".", and you'll have the integer part and the fractional part.
You've got the method for handling the integer part (don't forget to convert to integer!), so you just need to come up with something for the fractional part.

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.