-3

Need to print this number format as mentioned below in text format example: 1234567890 output: onetwothree....so on!

is it possible to do this?

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Abbx
  • 11
  • 3
    Java has no built-in functionality for this, but it's easy to do on your own. – Joachim Sauer Sep 01 '21 at 15:27
  • 2
    Yes, it is possible. Convert the number to a String, iterate over each character, convert it using a map or switch or if/else. – f1sh Sep 01 '21 at 15:28
  • Just a heads up @Abbx that many times when a homework looking question is posted on StackOverflow seemingly asking for the answer, it is heavily downvoted – Michael Smith Sep 01 '21 at 15:32

3 Answers3

0

You could try using Hashmap: https://www.w3schools.com/java/java_hashmap.asp

For each digit, make a key-value pair, integer to string, then use a loop to iterate through your string with numbers, comparing the number to the HashMap, and append value matching provided key to result string

public static void main(String[] args) {
    
    HashMap<Integer, String> numberWords = new HashMap<Integer, String>();
    numberWords.put(1, "One");
    numberWords.put(2, "Two");
    numberWords.put(3, "Three");
    numberWords.put(4, "Four");
    numberWords.put(5, "Five");
    numberWords.put(6, "Six");
    numberWords.put(7, "Seven");
    numberWords.put(8, "Eight");
    numberWords.put(9, "Nine");
    numberWords.put(0, "Zero");
    
    String numbers = "1234523";
    String result = "";
    
    for(int i =0; i< numbers.length(); i++) {
        char current = numbers.charAt(i);
        result = result + " " + numberWords.get(Character.getNumericValue(current));
    }
    
    System.out.println(result);

}

Although, as the first comment says, this does look like homework problem, and hashMap may be a bit of an overkill

Aden
  • 13
  • 4
  • This question feels like a homework problem for a beginner, meaning a HashMap will be too complex for this level. Iterating with a for/foreach loop with a bunch of if statements will probably be the solution – Michael Smith Sep 01 '21 at 15:31
0

Computers do not understand text.

There's nothing, in Java SE Specification, that can convert number to its respective word.

One way to achieve what you want, is to use java.util.HashMap<K, V>, but better would be to use array, as:

String[] numWords = new String[] {"zero", "one", ... "hundred"};

public String convertNumberToWord(int n) {

    return numWords[n];

}

However, this doesn't make much sense to implement such a method. You'll need to create or load dynamically all the string words, which you can't do, and even is not reasonable thing to do, imho.

To be honest, I can't even think of anything, at all, in computer science world, that can provide such a functionality, because, numbers are stored as binary values, and characters are also number codes (from the Unicode). Computers do not understand texts.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
0

To my knowledge, there's no library that does this, but it's a simple enough method to convert a string of digits into words:

String convertStringToNumbers(String input) {
    String result = "";
    for(int i = 0; i < input.length(); i++) {
        int currentDigit = Character.getNumericValue(input.charAt(i));
        switch (currentDigit) {
            case 0:
                result += "zero";
                break;
            case 1:
                result += "one";
                break;
            //add cases for the other digits
        }
    }
    return result;
}