0

How to write a program that the user enters a number as input (the number can be up to six digits) and the program converts it to text?

For example, the user enters 100 and prints output "one hundred".

The code must be in Java. The user can enter up to six digits at random

I wrote a code as follows, but it is for two digits, and for more digits, I stayed in the way of writing it

public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the number");
    int n=sc.nextInt();
    int n1=n,n2=n;
    int b=n1%10,a=n2/10; //  n1/10 means last digit is removed and n2%10 means last digit by modulus

    String[] single_digits = new String[]{"zero","one","two","three","four","five", "six","seven","eight","nine"};
    String[] two_digits = new String[]{"","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"};
    String[] tens_multiple = new String[]{"","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety"};
    if(a==1)
    {
        System.out.println(two_digits[b+1]);
    }
    else if(b==0)
        System.out.println(tens_multiple[a]);
    else
        System.out.println(tens_multiple[a]+"-"+single_digits[b]);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Abolfazl
  • 3
  • 4

1 Answers1

0

CRACKED

All you need are reusable methods. e.g a method to print 1 digit number, 2 digit number and so on... then you call printOne in printTwo after printing the first as shown...

import java.util.Scanner;

public class x {

    // Instance fields are below....
    // Placed below for easy readability of code...

    public static void main (String args[]) {

        // You know what this does...
        System.out.print (" Input number\n > ");
        Scanner sc = new Scanner (System.in);

        // Create int array to collect all digits...
        String number = Integer.toString (sc.nextInt ());
        int[] digits = new int[number.length ()];

        for (int i = 0; i < number.length (); i++)
            digits [i] = Integer.parseInt (number.charAt (i) + "");

        // All you need is a simple method to print a given amount..even up to ten million
        switch (digits.length) {
            case 1:
                printOne (digits);
                break;
            case 2:
                printTwo (digits);
                break;
            case 3:
                printThree (digits);
                break;
            case 4:
                printFour (digits);
                break;
            case 5:
                printFive (digits);
                break;
            case 6:
                printSix (digits);
                break;
        }
    }

    static void printOne (int[] arr) {
        // Just print coresponding value, easy since i converted to int[]
        System.out.print (below_ten [arr [0]]);
    }
    static void printTwo (int[] arr) {
        // check if it is a ~teen/not (you know what i mean)
        if (arr [0] == 1)
            System.out.print (below_twenty [arr [1] + 1]);
        else {
            System.out.print (below_hundered [arr [0]] + " ");
            if (arr [1] != 0) // added this to prevent 30 = thirty zero
                printOne (new int[] {arr [1]}); // this wont be needed for the ~teens
        }
    }
    static void printThree (int[] arr) {
        // now everything is siple, just print first digits and call previous, to call previous...
        // you get where that goes...
        System.out.print (below_thousand [arr [0]] + " ");
        printTwo (new int[] {arr [1], arr [2]});
    }
    // Now you have the algorithm, Challenge=print up to ten million... :-)
    static void printFour (int[] arr) {
        // challenge
    }
    static void printFive (int[] arr) {
        //exercise
    }
    static void printSix (int[] arr) {
        //bounty :-b)
    }

    // Placed below for easy readability of code...
    static String hundred = " hundred";
    static String[] below_ten = new String[] {
        "zero","one","two","three","four","five", "six","seven","eight","nine"
    };
    static String[] below_twenty = new String[] {
        "","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"
    };
    static String[] below_hundered = new String[] {
        "","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety"
    };
    static String[] below_thousand = new String[] {
        "", below_ten [1] + hundred, below_ten [2] + hundred,  below_ten [3] + hundred,  below_ten [4] + hundred,  below_ten [5] + hundred,  below_ten [6] + hundred,  below_ten [7] + hundred,  below_ten [8] + hundred,  below_ten [9] + hundred,
    };
}

_Good luck...

edit In case you have trouble with the other methods....works more than charm....

static void printFour (int[] arr) {
        System.out.print (below_million [arr [0]]);
        printThree (new int[] {arr [1], arr [2], arr [3]});
    }
    static void printFive (int[] arr) {
        printTwo (new int[] {arr [0], arr [1]});
        System.out.print (thousand);
        printThree (new int[] {arr [2], arr [3], arr [4]});
    }
    static void printSix (int[] arr) {
        printThree (new int[] {arr [0], arr [1], arr [2]});
        System.out.print (thousand);
        printThree (new int[] {arr [3], arr [4], arr [5]});
    }
static String thousand = " thousand ";
    static String[] below_million = new String[] {
        "", below_ten [1] + thousand, below_ten [2] + thousand,  below_ten [3] + thousand,  below_ten [4] + thousand,  below_ten [5] + thousand,  below_ten [6] + thousand,  below_ten [7] + thousand,  below_ten [8] + thousand,  below_ten [9] + thousand,
    };
Jore
  • 322
  • 2
  • 8
  • Thank you very much for your help But the code gives an error in the following part : static String thousand = " thousand "; static String[] below_million = new String[] { "", below_ten [1] + thousand, below_ten [2] + thousand, below_ten [3] + thousand, below_ten [4] + thousand, below_ten [5] + thousand, below_ten [6] + thousand, below_ten [7] + thousand, below_ten [8] + thousand, below_ten [9] + thousand, }; error : Usage of static non-final variable during initialization. How to fix the error? – Abolfazl Jan 31 '21 at 10:03
  • @Abolfazl Okay, that was just a toy program, get the original [here](https://github.com/jorexdeveloper/NumberToWordUnlimited/) with all instructions and also **DOES NOT HAVE A LIMIT**. – Jore Feb 03 '21 at 10:14
  • Thank you very much, my problem was solved. – Abolfazl Feb 03 '21 at 20:43