亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java TwitchBot/PircBot 無法讀取 .txt 文件中的命令,就像它必須做的那樣

Java TwitchBot/PircBot 無法讀取 .txt 文件中的命令,就像它必須做的那樣

喵喔喔 2023-05-17 15:46:38
我開始TwitchBot在java. 機器人工作正常,所以我的想法是用變量替換硬編碼命令。命令和消息保存在文本文件中。BufferedReader班級:try {            reader = new BufferedReader(new FileReader("censored//lucky.txt"));            String line = reader.readLine();            while (line != null) {                String arr[] = line.split(" ", 2);                command = arr[0];                message = arr[1];                line = reader.readLine();            }            reader.close();        } catch (IOException e) {            e.printStackTrace();        }我的片段bot/command classif(message.toLowerCase().contains(BufferedReader.command)) {            sendMessage(channel, BufferedReader.message);        }我的.txt文件:!test1 Argument1 Argument2!test2 Argument1 Argument2!test3 Argument1 Argument2!test4 Argument1 Argument2當我的文本文檔中只有 1 行時一切正常command+message / line,但是當有多行時,我無法訪問Twitch Chat. 我知道,命令是這樣堆疊的!test1 !test2 !test3 !test。所以我的問題是,我該如何避免這種情況?我擔心的是,在我的實際代碼中!test3使用了來自我的命令的消息!test1。
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

while (line != null) 

{

   String arr[] = line.split(" ", 2);

   command = arr[0];

   message = arr[1];

   line = reader.readLine();

}

此循環不斷讀取文件中的每一行并覆蓋commandand的內容message,這意味著當文件中有多個命令時 - 只有最后一行占上風。


如果要存儲多個命令/消息,則command/變量必須是ormessage類型。然后你可以根據內容進行匹配。java.util.ListHashMap


例如。,


Map<String,String> msgMap = new HashMap<>();

while (line != null) 

    {

       String arr[] = line.split(" ", 2);

       if(arr[0]!=null)

         msgMap.put(arr[0],arr[1]);

       line = reader.readLine();

    }


查看完整回答
反對 回復 2023-05-17
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號