gpt4 book ai didi

java - 将数字单词替换为数字

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:10:30 25 4
gpt4 key购买 nike

我正在尝试将所有数字单词 0-9 替换为字符串中相应的数字 0-9。

我创建了两个数字单词和数字数组,然后尝试使用替换循环来更改句子。

不确定我做错了什么。

我正在引用我的 getNoDigitWordString 方法。

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]");
}

关于java - 将数字单词替换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22924974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com