本文介绍了将数字单词替换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将所有数字单词 0-9 替换为字符串中相应的数字 0-9.
I am trying to replace all digit words zero-nine into their corresponding digits 0-9 in a string.
我创建了两个数字单词和数字数组,然后尝试使用替换循环来更改句子.
I have created the two arrays of digit words and digits and then trying for loop with a replace to change the sentence.
不确定我做错了什么.
我正在引用我的 getNoDigitWordString
方法.
I'm referencing my getNoDigitWordString
method.
import java.util.*;
public class StringProcessor {
private String noSpaces;
private String input, noVowels;
private String noDigitWords;
private int numOfWords = 0, uppercaseLetters = 0,
numOfDigits = 0, digitWords = 0;
private String [] wordDigits = {"zero","one", "two", "three","four",
"five", "six", "seven", "eight", "nine"};
private String [] digits = {"0", "1", "2", "3", "4",
"5", "6", "7", "8", "9"};
public StringProcessor()
{
input = "";
}
public StringProcessor(String s)
{
StringTokenizer str = new StringTokenizer(s);
numOfWords = str.countTokens();
for (int i = 0; i < s.length(); i++)
{
if (Character.isUpperCase(s.charAt(i)))
uppercaseLetters++;
}
for (int i = 0; i < s.length(); i++)
{
if (Character.isDigit(s.charAt(i)))
numOfDigits++;
}
String [] strSplit = s.split(" ");
for(int i = 0; i < strSplit.length; i++)
{
for (int j = 0; j < wordDigits.length; j++)
{
if (strSplit[i].equalsIgnoreCase(wordDigits[j]))
digitWords++;
}
}
noSpaces = s.replace(" ","");
noVowels = s.replaceAll("[aeiou]", "-");
for(int i = 0; i < 10; i++)
{
noDigitWords = s.replace("wordDigits[i]", "digits[i]");
}
}
public void setString(String s)
{
input = s;
}
public String getString()
{
return input;
}
public int wordCount()
{
return numOfWords;
}
public int uppercaseCount()
{
return uppercaseLetters;
}
public int digitCount()
{
return numOfDigits;
}
public int digitWordCount()
{
return digitWords;
}
public String getNoSpaceString()
{
return noSpaces;
}
public String getNoVowelString()
{
return noVowels;
}
public String getNoDigitWordString()
{
return noDigitWords;
}
public static void main(String[] args)
{
String input;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a line of text: ");
input = keyboard.nextLine();
StringProcessor str = new StringProcessor(input);
System.out.println("words: " + str.wordCount());
System.out.println("uppercase: " + str.uppercaseCount());
System.out.println("digits: " + str.digitCount());
System.out.println("digit words " + str.digitWordCount());
System.out.println("line with no spaces: " + str.getNoSpaceString());
System.out.println("line with vowels replaced: " + str.getNoVowelString());
System.out.println("line with digit words replaced: " + str.getNoDigitWordString());
}
}
谢谢.
推荐答案
这就是您要找的:
noDigitWords = s;
for(int i = 0; i < strSplit.length; i++)
{
for (int j = 0; j < wordDigits.length; j++)
{
if (strSplit[i].equalsIgnoreCase(wordDigits[j])){
noDigitWords = noDigitWords.replace(strSplit[i], digits[j]);
break;
}
}
}
取而代之的是:
for(int i = 0; i < 10; i++)
{
noDigitWords = s.replace("wordDigits[i]", "digits[i]");
}
这篇关于将数字单词替换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!