首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建包含不同长度、大小写和字符的错误修复方法

创建包含不同长度、大小写和字符的错误修复方法
EN

Stack Overflow用户
提问于 2015-11-29 05:39:43
回答 2查看 240关注 0票数 0

我的任务是创建一个String方法,它将接受任意长度和随机大写的输入,并返回一个字符串,其中第一个字母为大写,其余字符串为小写。

由于我们正在建立的程序是纸牌,这种方法应该返回输入,如“艺术”,“心”,“心”等作为“心”,这将是相同的其他三套西装。

我已经想出了如何纠正那些随机大写的输入,但是对于那些长度不同或者有排字的输入,我不知道。下面的代码是我试图改变输入的地方。

代码语言:javascript
复制
    /**
 * Converts the given string to title case, where the first
 * letter is capitalized and the rest of the string is in
 * lower case.
 * 
 * @param s a string with unknown capitalization
 * @return a title-case version of the string
 */
public static String toTitleCase(String s)
{
    String result = "";

    result = s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase();

    return result;
}

下一个代码是我用来测试上面代码的代码。

代码语言:javascript
复制
    private void main()
{
    testToTitleCase("HEARTS", "Hearts");
    testToTitleCase("hEarts", "Hearts");
    testToTitleCase("HEART", "Hearts");
    testToTitleCase("hEaRtS", "Hearts");
    testToTitleCase("heartss", "Hearts");
}

private void testToTitleCase( String input, String expectedOutput)
{
    String actualOutput = Card.toTitleCase(input);

    System.out.println("Testing whether toTitleCase('" + input + 
        "') returns '" + expectedOutput + "'.");

    if (expectedOutput.equals(actualOutput))
    {
        System.out.println("Success!");
    }

    else
    {
        System.out.println("Failure! The actual output was '" 
            + actualOutput + "'");
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-29 05:55:11

要纠正键入错误,可以将字符串的离散字符与原始字符串匹配,并计算匹配数并相应实现。例如:

代码语言:javascript
复制
public static String verifyInput(String typo) {

    String correctString[] = { "Spades", "Hearts", "Ace", "Club" };

    // A number to store maximum matches
    int maxMatch = 0;

    // Final String
    String finalStr = "";

    // Iterate through array to find best match
    for (String corrStr : correctString) {
        int match = 0;
        String dis = "";

        // Form discrete string
        // We match only discrete characters
        for (char x : typo.toCharArray())
            if (corrStr.toLowerCase().contains(String.valueOf(x).toLowerCase())
                    && !dis.contains(String.valueOf(x))) {
                dis += x;
                match++;
            }

        // Special stuff for ace
        if (corrStr.equals("Ace") && match > corrStr.length() / 2 && typo.length() == 3)
            return corrStr;

        // If the current string had more matches than previous, replace
        // previous with current
        if (match > maxMatch) {
            maxMatch = match;
            finalStr = corrStr;
        }
    }

    return finalStr;
}

输入:heartssaartsHEARTSaaarhzzzzsfartshusthusahuiu

输出:Hearts

输入:spSUgyDz

输出:Spades

输入:ACD

输出:Ace

票数 0
EN

Stack Overflow用户

发布于 2015-11-29 06:02:49

计算输入字符串与每个可能的法律值(“心”、“俱乐部”、“钻石”、“黑桃”)之间的Levenshtein距离。这个非常有用的阿帕奇公-朗库包含了一个用于此的函数。距离最低的一对可能是对的--但一定要确定距离的上限;如果用户输入“芥末黑麦上的寿司”,那么其中一个值将比其他值更接近匹配,但这并不意味着这是正确的匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33979770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档