简体   繁体   中英

Convert integer from 0-10 and replace with English word that corresponds to integer

Here in this code when 10 occurs in the input it is printing one zero instead of printing ten.

How can I resolve this problem?

import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        String phrase = scn.nextLine();
        String[] digits = {"0","1","2","3","4","5","6","7","8","9","10"};
        String[] alphabets = {"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
        for (int i=0; i<digits.length; i++) {
            phrase = phrase.replaceAll(digits[i],alphabets[i]+"");
        }
        System.out.println(phrase);
    }
}

Ten is the first "digit" that is actually two digits. You change zero and one first. Change ten first instead to handle that case, (or you could make it A instead of "10" - you seem to have base eleven here).

String[] digits = { "10", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
String[] alphabets = { "ten", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

This is because of incorrect iteration method, you need to iterate in reverse.

    public class Sample {
public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    String phrase = scn.nextLine();
    String[] digits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    String[] alphabets = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    for (int i = digits.length - 1; i >= 0; i--) {
        phrase = phrase.replaceAll(digits[i], alphabets[i] + "");
    }
    System.out.println(phrase);
}

}

Your problem is that you process each character in the input, probably without spaces, instead of "words" separated by spaces.

You can split the input into words by doing String[] numbers = phrase.split(" ");, and then create a new return string with each number replaced by the corresponding word.

You should probably use a hashmap instead of two arrays as well.

A workaround : Replace "onezero" with "ten".

Demo:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        String phrase = scn.nextLine();
        String[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
        String[] alphabets = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
        for (int i = 0; i < digits.length; i++) {
            phrase = phrase.replace(digits[i], alphabets[i]).replace("onezero", "ten");
        }
        System.out.println(phrase);
    }
}

A sample run:

hello10world
hellotenworld

Note that you should use replace instead of replaceAll which is used with regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM