0
How to extract a variable's value outside of a block?
I need to get the mm value outside of the loop block. The mm is declared earlier in the code, but still the compiler says it's not initialized. The code goes as follows: char mi; //already initialized and known char [] init; // already initialized with references int mm; // only declared for (int x=0; x<5; x++) // the loop { if (init[x]==mi) mm=x+1; //assigning the value } System.out.println(mm); //error produced Do you know why this happens and how to get it fixed?
6 Réponses
+ 3
Have you tried initalizing mm with a value before using it in the for loop that might be the problem as variables must be initalized with some value before being used..
+ 3
jasco 😉 java never stops giving, I have had many moments like this a few years ago. good luck 👍
+ 2
Seems like some code is missing because I cant see what " init[x] is ment to be"?
+ 1
This helped, thanks. Learning something new every day :)
0
init[x] refers to char array also initialized earlier. You're right, I didn't include it in my code, which could be misleading. Code edited now.
0
local variables must to have value before using, compiler doesn't know if for() condition is true