Why is my java function not calling from the output of another class?

Tags: , ,



I’m coding a discord bot to be used within a private server just to mess around with. It is my first experience with java. I am using the Discord JDA Library to code the bot. I don’t think that is the main issue, however.

I’m confused as to how I am supposed to pull output from a specific method within a separate class I have created.

I am trying to pull a String from a public String method within a separate class called Color.java into a file called Commands.java. This string is meant to be randomized by using an Array List with a random number generator.

Here is my code for Commands.java. This is not the main file but the one that the issue pertains to, more specificly the final else if {} of this code.

public class Commands extends ListenerAdapter {
    @Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {

        String[] args = event.getMessage().getContentRaw().split(" ");

        if (args[0].equalsIgnoreCase(bot.prefix + "info")) {
            event.getChannel().sendTyping().queue();
            event.getChannel().sendMessage("This is a test info description").queue();
        }

        else if (args [0].equalsIgnoreCase(bot.prefix + "ping")) {
            long ping = event.getJDA().getGatewayPing();

            event.getChannel().sendMessage(ping + "ms").queue();
        }

        else if (args [0-100].equalsIgnoreCase("white")){
            
            Race newColorobj = new Color();
            String white_test = newColorobj.white();
            event.getChannel().sendMessage(white_test + ".").queue();
        }
    }
}

I intended for the final “else if” to pull from this file, Color.java, pick out a random string of text from the array list “white”, and output it into the discord chat channel.

public class Color {

    Random rand = new Random();
    int upperbound = 1;

    int int_random = rand.nextInt(upperbound);

    public String white() {

        ArrayList<String> white = new ArrayList<String>();

        white.add("This is a test");

        return white.get(int_random);

    }
}

My terminal outputs this error when compiling, but it still succeeds and runs:

white : The term 'white' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try      
again.
At line:1 char:1
+ white c:; cd 'c:UsersColinDylan and Colin'; & 'c:UsersColin.vsc ...
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (white:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

if the user tries to say “white” to the bot, it does not respond with the string of text I intended it to respond with.

I’m still new to Java. What am I doing wrong?

Answer

Solution found: inside of Color.java, I needed to change public String white(){} to public String white(String… args){}.

public class Color {

    Random rand = new Random();
    int upperbound = 1;

    int int_random = rand.nextInt(upperbound);

    public String white(String... args) {

        ArrayList<String> white = new ArrayList<String>();

        white.add("This is a test");

        return white.get(int_random);

    }
}


Source: stackoverflow