Solved StringBuilder to string help:(

Discussion in 'Spigot Plugin Development' started by ElbestHD, Sep 21, 2018.

  1. Hi, good afternoon people from spigot, I have a problem with this code, it will be that you can help me solve it, please
    4Po_Yr_G.jpg
     
  2. what even are you doing, jesus
     
    • Agree Agree x 1
  3. Whats the problem? What are you trying to accomplish? Can you paste the code into a paste service instead of posting a screenshot?
     
    • Agree Agree x 1
  4. A StringBuilder is not a String so you can't assign a reference of a StringBuilder to a String. Use sb2.toString().

    Also, what on God's green earth are you doing, lol.
     
  5. In the first look you have the sb2 underlined in red
     
  6. code:
    Code (Text):
    public class ScoreboardUtil
    {
      private Scoreboard scoreboard;
      private Objective objective;
      private boolean reset;
     
      public String color(String s)
      {
        return s.replaceAll("&", "§");
      }
     
      public ScoreboardUtil(String s, String s2, Boolean b)
      {
        this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
        (this.objective = this.scoreboard.registerNewObjective(s2, "dummy")).setDisplayName(s);
        this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
      }
     
      public void setName(String substring)
      {
        if (substring.length() > 32) {
          substring = substring.substring(0, 32);
        }
        this.objective.setDisplayName(color(substring));
      }
     
      public void lines(Integer n, String substring)
      {
        Team team = this.scoreboard.getTeam("TEAM_" + n);
        if (substring.length() > 32) {
          substring = substring.substring(0, 32);
        }
        String[] splitStringLine = splitStringLine(substring);
        if (team == null)
        {
          Team registerNewTeam = this.scoreboard.registerNewTeam("TEAM_" + n);
          registerNewTeam.addEntry(getEntry(n));
          setPrefix(registerNewTeam, splitStringLine[0]);
          setSuffix(registerNewTeam, splitStringLine[1]);
          this.objective.getScore(getEntry(n)).setScore(n.intValue());
        }
        else
        {
          setPrefix(team, splitStringLine[0]);
          setSuffix(team, splitStringLine[1]);
        }
      }
     
      public void setPrefix(Team team, String prefix)
      {
        if (prefix.length() > 16)
        {
          team.setPrefix(prefix.substring(0, 16));
          return;
        }
        team.setPrefix(prefix);
      }
     
      public void setSuffix(Team team, String s)
      {
        if (s.length() > 16) {
          team.setSuffix(maxChars(16, s));
        } else {
          team.setSuffix(s.substring(0, s.length()));
        }
      }
     
      public String maxChars(int n, String s)
      {
        if (ChatColor.translateAlternateColorCodes('&', s).length() > n) {
          return s.substring(0, n);
        }
        return ChatColor.translateAlternateColorCodes('&', s);
      }
     
      public String getEntry(Integer n)
      {
        if (n.intValue() == 0) {
          return "§0";
        }
        if (n.intValue() == 1) {
          return "§1";
        }
        if (n.intValue() == 2) {
          return "§2";
        }
        if (n.intValue() == 3) {
          return "§3";
        }
        if (n.intValue() == 4) {
          return "§4";
        }
        if (n.intValue() == 5) {
          return "§5";
        }
        if (n.intValue() == 6) {
          return "§6";
        }
        if (n.intValue() == 7) {
          return "§7";
        }
        if (n.intValue() == 8) {
          return "§8";
        }
        if (n.intValue() == 9) {
          return "§9";
        }
        if (n.intValue() == 10) {
          return "§a";
        }
        if (n.intValue() == 11) {
          return "§b";
        }
        if (n.intValue() == 12) {
          return "§c";
        }
        if (n.intValue() == 13) {
          return "§d";
        }
        if (n.intValue() == 14) {
          return "§e";
        }
        if (n.intValue() == 15) {
          return "§f";
        }
        return "";
      }
     
      public Scoreboard getScoreboard()
      {
        return this.scoreboard;
      }
     
      public void setReset(boolean reset)
      {
        this.reset = reset;
      }
     
      public boolean isReset()
      {
        return this.reset;
      }
     
      public void build(Player player)
      {
        player.setScoreboard(this.scoreboard);
      }
     
      private String[] splitStringLine(String s)
      {
        StringBuilder sb = new StringBuilder(s.substring(0, s.length() >= 16 ? 16 : s.length()));
        StringBuilder sb2 = new StringBuilder(s.length() > 16 ? s.substring(16) : "");
        if ((sb.toString().length() > 1) && (sb.charAt(sb.length() - 1) == '§'))
        {
          sb.deleteCharAt(sb.length() - 1);
          sb2.insert(0, '§');
        }
        String string = "";
        for (int i = 0; i < sb.toString().length(); i++) {
          if ((sb.toString().charAt(i) == '§') && (i < sb.toString().length() - 1)) {
            string = String.valueOf(string) + "§" + sb.toString().charAt(i + 1);
          }
        }
        String string2 = sb2;
        if (sb.length() > 14) {
          string2 = String.valueOf(String.valueOf(string)) + string2;
        }
        return new String[] { sb.toString().length() > 16 ? sb.toString().substring(0, 16) : sb.toString(), string2.toString().length() > 16 ? string2.toString().substring(0, 16) : string2.toString() };
      }
    }
     
  7. If you want some help you need to help us by explaining what you are trying to do.
     
  8. well.. sb2 is a StringBuilder and you try assigning it to a string that's why it's underlined. try sb2.toString();
     
    #8 darklazerog, Sep 21, 2018
    Last edited: Sep 21, 2018
  9. Good bro that is a sboreboard api that I passed a friend and from the beginning I got that error and I have to pass StringBuilder sb2 to string
     

  10. the same error keeps coming up, try it
     
  11. Whats the error?
     
    • Friendly Friendly x 1
  12. in the String string2 = sb2; sb2 comes out in red
     
  13. As stated by others, change "String string2 = sb2;" to "String string2 = sb2.toString();"
     
    • Agree Agree x 1
    • Friendly Friendly x 1
  14. lol thanks if it's already solved like: D
     
    • Funny Funny x 1
  15. wut.jpg
     
    • Agree Agree x 5
    • Funny Funny x 2