不能在与字符串声明相同的方法中替换字符串内容 JDA

如何解决不能在与字符串声明相同的方法中替换字符串内容 JDA

我正在尝试将随机生成的字符串存储在字符串变量中,以用于其他用途。基本上,它是一个密码生成器,用于存储密码以供需要它的命令使用。该应用程序似乎在存储密码以供以后使用时遇到问题。代码和输出与预期的相符。

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    super.onGuildMessageReceived(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();
            user.openPrivateChannel().complete()
                .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,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();
                user1.openPrivateChannel().complete()
                    .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();
            }
        }
}

由于某种原因,在//password generator部分中,stored被随机字符串替换了,但是当您使用!setpassword命令输入该密码时,stored是为空,因此即使replace具有该方法的适当范围,也好像没有保存在密码生成器部分中使用的StringBuilder方法。 我已经尝试了一段时间。我尝试使用String,StringBuilder和StringBuffer来查看是否有不同的结果,但输出都相同。

输出:

!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.

预期输出:

 !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

在我的脑海中,我想这可能是我生成随机密码并存储它的方式,但是我不确定,因为这似乎是字符串声明本身及其范围的问题

解决方法

尝试:

StringBuilder stored = new StringBuilder(15);

String stored = "";

OR

尝试将其添加到public void onGuildMessageReceived(GuildMessageReceivedEvent event)上方:

private String Stored = "";

每次要使用它时,请执行this.Stored

看看它是否行得通。

此外,尝试添加一条语句以在代码中的随机点向dms发送当前存储的字符串,这就是我所说的打印-调试以仅查看值何时更改以减少搜索。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?