How to extract a variable's value outside of a block? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

8th Jul 2020, 10:57 PM
jasco
jasco - avatar
6 Answers
+ 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..
8th Jul 2020, 11:16 PM
D_Stark
D_Stark - avatar
+ 3
jasco 😉 java never stops giving, I have had many moments like this a few years ago. good luck 👍
8th Jul 2020, 11:30 PM
D_Stark
D_Stark - avatar
+ 2
Seems like some code is missing because I cant see what " init[x] is ment to be"?
8th Jul 2020, 11:07 PM
D_Stark
D_Stark - avatar
+ 1
This helped, thanks. Learning something new every day :)
8th Jul 2020, 11:21 PM
jasco
jasco - avatar
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.
8th Jul 2020, 11:11 PM
jasco
jasco - avatar
0
local variables must to have value before using, compiler doesn't know if for() condition is true
9th Jul 2020, 2:43 AM
zemiak