Replacing digit words into digits

2019-07-13 02:22发布

问题:

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.

Not sure what i am doing wrong.

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());
}
}

Thanks.

回答1:

This is what you're looking for:

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;
        }
    }
}

Instead of this:

for(int i = 0; i < 10; i++)
{
    noDigitWords = s.replace("wordDigits[i]", "digits[i]");
}


回答2:

Aside from the problem @dcharms pointed out, this is not going to work:

for(int i = 0; i < 10; i++)
{
    noDigitWords = s.replace(wordDigits[i], digits[i]);
}

Here's what happens: First, you search s for "zero", and call s.replace. This returns a string with the word "zero" replaced by "0". Then that string is assigned to noDigitWords. But s itself does not change.

Then, next time through the loop, you search in the original s for the next word. The work you did in the previous iteration, which was in noDigitWords, gets thrown away.

The result will be that the last word you search for, "nine", should get replaced. But any other replacements you've made will be thrown away.



回答3:

Each time the following code calls the replace method, the string noDigitWords is overwritten.

for(int i = 0; i < 10; i++)
{
    noDigitWords = s.replace("wordDigits[i]", "digits[i]");
}

After the final iteration of the loop, the string "one nine" becomes "one 9" rather than "1 9" as desired. The solution is to call the replace method on the return value of the previous iteration of the loop. Initialize the string noDigitWords to the value of the string s. Each iteration do the replace on noDigitWords as shown below.

noDigitWords = s;
for(int i = 0; i < 10; i++)
{
    noDigitWords = noDigitWords.replace(wordDigits[i], digits[i]);
}


回答4:

Try replacing noDigitWords = s.replace("wordDigits[i]", "digits[i]"); with noDigitWords = s; outside for loop and noDigitWords = noDigitWords.replaceAll(wordDigits[i], digits[i]); inside.

Your original was looking for a string "wordDigits[i]" instead of the contents of wordDigits[i]. Your output had a similar issue.