Crate problem

Discussion in 'Plugin Development' started by Dubzay, Jun 18, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Dubzay

    I've been getting an error on .replace on the end of the long string of code. Any ideas why?
    Code:
    if (CrateController.crateExist(j))
                  {
                    str1 = SettingsManager.Messages.getString("success.removed").replace("[crate]", SettingsManager.Config.getString(new StringBuilder("crates.").append(j).append(".name").toString()) + " Crate").replace("[id]", j);
                    paramCommandSender.sendMessage(C.getPrefix() + C.inColor(str1));
                   
                    SettingsManager.Config.set("crates." + j, null);
                  }
                  else
                  {
                    str1 = SettingsManager.Messages.getString("error.crate-id-not-found");
                    paramCommandSender.sendMessage(C.getPrefix() + C.inColor(str1));
     
  2. Why you're using a StringBuilder ?
     
  3. Offline

    Dubzay

    It's a link to a command that removes the Id of the Crate
     
  4. Offline

    LeePMC

    Use .replaceAll instead of .replace if you don't mind replacing all of the [crate] in the string
     
  5. Offline

    Drkmaster83

    Uhhh, no, then you'd be using a regular expression that matches all of the characters in "crate" and replaces them. Bad news bears.

    You don't need to use a StringBuilder for something as simple as what you're doing. You can just do primitive String concatenation unless you're worried about thread-safety.
    Code:
    str1 = SettingsManager.Messages.getString("success.removed").replace("[crate]", SettingsManager.Config.getString("crates."+j+".name") + " Crate").replace("[id]", j);
    Additionally, it's hard to solve an error when you don't paste it or show us it.
     
  6. Offline

    LeePMC

    .replaceAll("[crate]", crateName) doesn't replace all the characters in crate it replaces ALL of the [crate]'s inside a string to the crate name so you don't have to use .replace multiple times if there are multiple [crate]'s in the string
     
  7. Offline

    Drkmaster83

    That's potentially the most ignorant thing I've heard. Look up the Java Documentation for the two methods, please.
    Replace:
    [​IMG]
    Yields:
    [​IMG]

    -----------------------------------------------------------------------------------
    ReplaceAll:
    [​IMG]
    Yields:
    [​IMG]
    ^ Notice how the 'R' is left alone because it is not the same character as 'r'.
     
Thread Status:
Not open for further replies.

Share This Page