简体   繁体   中英

How would I get a string from a different method if they are in the same class

Right, I understand that this question has more than likely been answered before however it won't work in my circumstance.

So I have a string that I need to get into another method, here's the code...

public String createColoredMinuses(int amount, ChatColor colorOne, ChatColor colorTwo)
{
StringBuilder builder = new StringBuilder();
builder.append(amount < 13);
if (amount % 2 == 0) {}
builder.append(ChatColor.BLUE + "-");
builder.append(ChatColor.DARK_BLUE + "-");
String string = builder.toString();
return string;
}

public boolean onCommand(CommandSender sender, Command cmd, String command,       String[] args)
{

  public static void createColoredMinuses()
  {
      Method1();
  }



  String b = builder.toString();

So what I want to happen is I want to be able to get the 'return string;' into the main part where all the commands go but into the 'String b=' part however I can't work out how to do it.

I hope this made sense to you! :P

Would it not be enough to have the builder as a private variable? I mean you literally want to use it privately in another method that you call later:

private StringBuilder builder = new StringBuilder();

public String createColoredMinuses(int amount, ChatColor colorOne, ChatColor colorTwo)
{
    builder.append(amount < 13);
    if (amount % 2 == 0) {}
    builder.append(ChatColor.BLUE + "-");
    builder.append(ChatColor.DARK_BLUE + "-");
    String string = builder.toString();
    return string;
}

public boolean onCommand(CommandSender sender, Command cmd, String command,       String[] args) {

    public static void createColoredMinuses ()
    {
        Method1();
    }


    String b = builder.toString();
}

Another noticeable thing is that you have this method:

public static void createColoredMinuses ()
        {
            Method1();
        }

And it's inside the onCommand... this doesn't look right. You need to move this to somewhere else.

In the end, I just moved the StringBuilder as I couldn't work out how to call the method. So that it was in the class with everything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM