The following portion of Java code scans the string given to find sequence of numbers, either individual or grouped. But the problem I find is that I need to write the name of the numbers as individual and not as grouped. That is, if the string has "Hello78, my name is number07 and not number1", instead of printing as "Seventy Eight", how can I get it to be "Seven Eight"?
Pattern patternString = Pattern.compile("\\d+");
String inputText = "32h28ello12'34' this is a test89 87";
Matcher findNumber = patternString.matcher(inputText);
Scanner findText = new Scanner(inputText);
findText.useDelimiter("[^\\p{Alnum},\\.-]");
int counter = 0;
List<String> list = new ArrayList<>();
while (findNumber.find()) {
list.add(findNumber.group());
System.out.println(findNumber.group(0));
}
So, this is an example of the output just to guide myself:
32
28
12
34
89
87
-----------------------------------------------
LIST: [32, 28, 12, 34, 89, 87]
32h28ello12'34' this is a test89 87
-----------------------------------------------
I am using the line below to continue testing and to replace the number for the text, but then I realize that I need to replace the whole number in that position. That is, if it is 30, then the text is "Three Zero" and not "Three" alone or "Zero" alone.
numString = inputText.replace(Character.toString(inputText.charAt(atPos)), one);
Some of the ideas I have had are for example, putting each output number in another variable, the read it back, then split it (each) and writing the number's name, but I can't figure out how to do it. Any idea will be appreciated.
Character.isDigit(ch)to test each one to see if it's a digit. If not, just append it to your result string. If it is a digit, useCharacter.digit(ch,10)to obtain its numeric value, then use that value to index an array ofStrings containing the digit names and append that text to your result.Map<Character, String>that contains the 10 digits character as key and there string counterpart as value. Then just traverse the string and replace the digits with the value associated in map??