D
D
Dropych2020-12-11 12:55:01
Java
Dropych, 2020-12-11 12:55:01

JAVA How to get variable value from for?

How to pass each numberS value from the send method?
These are buttons for telegrams, they are formed by the for loop. I want them to update the record in the database on click, for this I want to pass the value of the numberS variable. How to do it right?

public void send(){
String[] greportsArr = new String[greportsL.size()];
greportsArr = greportsL.toArray(greportsArr);
for(int i = 0; i < numL.size(); i++){
    numberL = numL.get(i);
    String reportsL = greportsArr[i];
    String[] numLArr = new String[numL.size()];
    numLArr = numL.toArray(numLArr);
    numberS = numLArr[i];
for(int it = 0; it < userids.size(); it++){
    String userid = userids.get(it);        
  ------
inlineKeyboardButton1.setText(numberS);
inlineKeyboardButton1.setCallbackData("Agree");
----
System.out.println(numberS);  // тут значение еще верное

try {
   execute(message);
} catch (TelegramApiException e) {
    e.printStackTrace();
}
}   }
   }


public void onUpdateReceived(Update update) {
----
} else if (update.hasCallbackQuery()) {
        String call_data = update.getCallbackQuery().getData();
    long message_id = update.getCallbackQuery().getMessage().getMessageId();
    long chat_id = update.getCallbackQuery().getMessage().getChatId();
    System.out.println(numberS);   // тут уже приходит последнее значение в массиве.
    if (call_data.equals("Agree")) {
        Login lgc = new Login();
         lgc.updateQ(numberS);
            String answer = "Согласовано";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2020-12-11
@WaterSmith

You

System.out.println(numberS);  // тут значение еще верное
output in a loop, and
System.out.println(numberS);   // тут уже приходит последнее значение в массиве.
in a completely different way.
numberS is, apparently, an attribute of the class, otherwise it would not be visible to you in the onUpdateReceived method.
And why are you surprised? In your send method, with each iteration of the loop, the value of numberS changes, therefore, after the last iteration, the last value is there.
Unfortunately, I can’t tell you how to solve your problem correctly, because you didn’t describe the problem, and it’s also difficult to understand from the code what it should have done.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question