Skip to content

String contents not being replaced within the same method as string declaration. (JDA)

I am trying to store a randomly generated string in a string variable to use for in for other things. Basically, it is a password generator that stores the password for use with commands that need it. The application seems to have problems with storing the password for later use though. Code and output vs expected below.

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {

    String id = "discord userid String";
    long idlong = Long.parseLong(id);
    String generatedString = RandomStringUtils.random(15, true, true);
    StringBuilder stored = new StringBuilder(15);

   //password generator
    if (event.getMessage().getContentRaw().equalsIgnoreCase("!passwordgen")) {
        if (stored.toString().isEmpty() == true || idlong == 
        event.getMessage().getAuthor().getIdLong()) {
            stored.replace(0, 14, generatedString);

            User user = event.getMessage().getAuthor();
                .sendMessage("Your new password is: " + stored + ". You can change your password 
                in the future with !setpassword <currentpassword>.").queue();
            else {
                event.getChannel().sendMessage("You do not have permission to generate a new password  
                and/or an initial password has already been set.").queue();
        //set password command      
        if (event.getMessage().getContentRaw().startsWith("!setpassword")) {    
            char[] chararr = event.getMessage().getContentRaw().toCharArray();
            for (int i = 0; i < chararr.length; i++) {
                if (chararr[i] == ' ') {
                    passwordkeyed += event.getMessage().getContentRaw().substring(13);
            if (passwordkeyed.equals(stored.toString()) && !(passwordkeyed.isEmpty())) {
                stored.replace(0, 14, generatedString); //replaces random password from !passwordgen 
               // with new random password
                event.getChannel().sendMessage("Stored: " + stored).queue();
                event.getChannel().sendMessage("Check your DMs!").queue();
                User user1 = event.getMessage().getAuthor();
                    .sendMessage("Your new password is: " + stored).queue();  
                if (!(passwordkeyed.equals(stored.toString()))) {
                event.getChannel().sendMessage("Incorrect password. Stored: " + stored).queue();
                if (stored.toString().isEmpty()) {
                event.getChannel().sendMessage("Please use !passwordgen to generate an initial password.").queue();

For some reason, in the //password generator section, stored is replaced with a random string just fine but when you enter that password using the !setpassword command, stored is empty so it is like the replace method I used in the password generator section is not being saved even though the StringBuilder has the proper scope for the method. I’ve been trying to do this for a bit now. I have tried with String, StringBuilder, and StringBuffer to see if any had different results but all had the same output.


!passwordgen //discord bot command

Your new password is: h50uSKlrWa30Q40. You can change your password in the future with !setpassword 
<currentpassword>. //private message to whoever ran the command

!setpassword h50uSKlrWa30Q40 //discord bot command

Incorrect password. Stored:
Please use !passwordgen to generate an initial password.

Expected Output:

 !passwordgen //discord bot command

Your new password is: h50uSKlrWa30Q40. You can change your password in the future with !setpassword 
<currentpassword>. //private message to whoever ran the command

!setpassword h50uSKlrWa30Q40 //discord bot command

Stored: 2Fx8buVxpIEyNYX
Check your DMs!

Your new password is: 2Fx8buVxpIEyNYX //private message to whoever ran the command

In the back of my mind I am thinking it may be how I am generating the random password and storing it but I am not sure since this seems to be a problem with the string declaration itself and its scope




StringBuilder stored = new StringBuilder(15);


String stored = "";


try to add this above the public void onGuildMessageReceived(GuildMessageReceivedEvent event):

private String Stored = "";

Everytime you want to use it, do this.Stored

See if it works then.

Also, try to add a statement to send in dms the current stored string at random points in the code this is what i call print–debugging to simply see when the value changes to reduce our search.

6 People found this is helpful