Answer the question
In order to leave comments, you need to log in
Can't assign number inside for loop in Java
There was a task in Java, in which I am not particularly strong. It is necessary to assign a variable that is outside the block. How to do it? Tried as below, but nothing gets logged:
int index=0;
for( int j=0; j<passwords.length;j++)
{
if(passwords[j].equals(ov.getValue().toString()))
{
index = j;//Тут надо присвоить переменной, которая находиться вне блока. Как?
break;
}
}
Answer the question
In order to leave comments, you need to log in
The code looks correct. Are you sure that the condition is met?
And in general to you algorithmic decomposition to apply. Something like:
int findPasswordIndex(String passwords[], String password)
{
for( int j=0; j<passwords.length;j++)
{
if(passwords[j].equals(password))
{
return j;
}
}
// кинуть исключение - индекс не найден, или вернуть специальное значение
}
...
int index = findPasswordIndex(passwords, ov.getValue().toString());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question