【问题标题】:error message when doing if statement and removing white spaces执行 if 语句并删除空格时出现错误消息
【发布时间】:2016-06-21 04:52:32
【问题描述】:

您好,我的作业问题:

为name字段传递的参数不能为null, 并且不能是空字符串或仅组成的字符串 的空白。如果作为参数传递的参数是 3 null 或空/空白字符串,将字段设置为 DEFAULT_POOL_NAME 否则,格式化值 正确(删除空格,首字母大写, 将其余部分设置为小写),然后再将其存储在实例中 变量。

这是我的代码:

public Pool(String newName, double newVolumeLitres, double newTemperatureCelsius, double newPH, double newNutrientCoefficient) {
  if (newName != null && newName.trim().length() > 0) {
   name = formatNameTitleCase(newName.trim());
  } else {
   name = DEFAULT_POOL_NAME;
  }
}

我收到一条错误消息,指出

cannot find symbol - method formatNameTitleCase(java.lang.string)

【问题讨论】:

  • 抱歉粘贴不好。
  • 了解如何格式化您的代码
  • 公共池 (String newName, double newVolumeLitres, double newTemperatureCelsius, double newPH, double newNutrientCoefficient) { if (newName != null && newName.trim().length() > 0) { name = formatNameTitleCase (newName.trim()); } 其他 { 名称 = DEFAULT_POOL_NAME; }
  • 所以我收集到的是 formatNameTitleCase 不存在。
  • 我猜是你,但我不认为我只是想把首字母大写,其余小写

标签: java


【解决方案1】:

您可以使用第三方库来检查空格情况。 例如 : Apache common lang jar 有 StringUtils 类。 你可以使用

StringUtils.isNotBlank(String);

【讨论】:

  • 所以它的 StringUtils.isNotBlank(newName);
  • 不需要第三方库
  • 是的,在你的情况下,它将是 newName
【解决方案2】:

要将第一个字母大写,请将子字符串设置为大写,如下所示:

somestring.substring(0, 1).toUpperCase() + somestring.substring(1);

在你的情况下是:

String trimmed = newName.trim();

name = trimmed.substring(0, 1).toUpperCase() + trimmed.substring(1);

(我假设name 已经初始化)

substring() 获取字符串的某个指定部分并捕获它。 substring(0, 1) 将获取单词的第一个字符并将其大写为toUpperCase()。然后substring(1) 将从第一个字符开始获取字符串的其余部分,并将其与大写字母连接起来,从而产生以下输出。来自,

"examplestring"

到,

"Examplestring"

我还建议使用toLowerCase() 来确保其余部分是小写的。最后,我可能会做一个 try/catch 以确保字符串索引确实存在。 “故障安全”的最终代码:

String trimmed = newName.trim();
try {
    name = trimmed.substring(0, 1).toUpperCase() + trimmed.substring(1).toLowerCase();
} catch(StringIndexOutOfBoundsException e) {
    e.printStrackTrace();
    System.out.println("String was not at least one character!");
}

trim() 只去掉尾随空格,所以像这样使用replaceAll() 方法:

str.replaceAll("\\s+", "");

这将替换所有空格,包括换行符 (\n)。它使用正则表达式(正则表达式)来匹配所有空格并替换为空。

TL;DR

这是删除所有空格并将第一个字母大写的代码:

try {
    name = newName.substring(0, 1).toUpperCase() + newName.substring(1).toLowerCase();
} catch(StringIndexOutOfBoundsException e) {
    e.printStrackTrace();
    System.out.println("String was not at least one character!");
}
name.replaceAll("\\s+", "");

请考虑阅读全文。

应用

public String formatNameTitleCase(String oldName) {
    try {
        name = oldName.substring(0, 1).toUpperCase() + oldName.substring(1).toLowerCase();
    } catch(StringIndexOutOfBoundsException e) {
        e.printStrackTrace();
        System.out.println("String was not at least one character!");
        return "failed";
    }
    name.replaceAll("\\s+", "");

    return name;
}

【讨论】:

    【解决方案3】:

    这就是你想要的。

    public Pool(String newName, double newVolumeLitres, double newTemperatureCelsius, double newPH, double newNutrientCoefficient) {
        //Removing White spaces
        if(newName!=null) {
            newName = newName.replaceAll(" ", "");
        }
        //Check for empty and null
        if(newName==null || newName.isEmpty()) {
            newName=DEFAULT_POOL_NAME;
        }
        //Setting the first to uppercase and the rest to lower case
        else {
            newName = newName.substring(0, 1).toUpperCase() + newName.substring(1).toLowerCase();
        }
    }
    

    【讨论】:

    • 谢谢你 ahmed 但我不确定你从这里和雅虎打招呼时的意思?
    • 字符串变量是你的字符串可以是任何东西。 DEFAULT_POOL_NAME 也是您设置的任何默认名称。
    • 我只是向你展示,如果 name 是 "hello FROM HereE",输出将是 Hellofromhere,如果 name = null 或只有空格,结果将是 DEFAULT_POOL_NAME,这里是 yahoo
    • 哦,好吧,但我想将其设置为 DEFAULT_POOL_NAME。
    • 只有在空格和null的情况下才设置为DEFAULT_POOL_NAME。
    猜你喜欢
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 2019-12-24
    • 2020-02-10
    • 1970-01-01
    相关资源
    最近更新 更多