I have just start learning java and I'm sorry if my question is a bit nooby.
Can anyone tell me why this code gives an Error and how to fix it? thanks
static String test = "abc";
static String lower = "abcdefghijklmnopqrstuvwxyz" ;
static String re = "" ;
public static void main(String[] args) {
for (int i = 0 ; i < test.length() ; ++i ) {
char x = test.charAt(i);
int f = lower.indexOf(x);
int h = (f + 2) %26;
if (h <0) {
h = h + 26;
}
char r = lower.charAt(h);
String re = re + r ; /* here is the problem */
}
System.out.println(re);
}
}
output: The local variable re may not have been initialized
re += r;instead ofString re = re + r ;