文本处理器赋值setString方法java

时间:2014-04-14 23:41:55

标签: java

我在弄清楚为什么我的setMethod不起作用时遇到了问题。当我使用它时,我的主要方法没有显示任何正确的答案。我最初使用用户输入到我的StringProcessor str = new StringProcessor(输入),它完美地工作。我只是不确定为什么我不能让它工作,如果我没有在我的新对象输入并使用设置字符串来创建我的字符串。任何帮助都会很棒。感谢。

import java.util.*;
public class StringProcessor {
private String noSpaces, string;
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()
{
    string = ("");
}

public StringProcessor(String s)
{
    StringTokenizer one = new StringTokenizer(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]", "-");

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

public void setString(String s)
{
    string = s;
}

public String getString()
{
    return string;
}

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)     {

    StringProcessor str = new StringProcessor();
    str.setString("my string");
    System.out.println(str.getString());

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

}

1 个答案:

答案 0 :(得分:1)

问题在于,当您使用字符串参数构造StringProcessor时,所有计算都已完成:

StringProcessor str = new StringProcessor("some string");

但是你没有使用那个构造函数;你只使用不带参数的构造函数。因此,执行所有计算的代码永远不会被执行。

将计算移动到私有方法。然后让单参数构造函数和setString调用该方法。 (并确保setString或您的私有方法将所有计数重新初始化为0,这样,如果多次设置setString,则值将是正确的而不是累积的。)