Reputation: 65
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
Upvotes: 1
Views: 137
Reputation: 478
In your code, you look to be declaring the variable 're' twice. One at the class level(static) and once within the method. You can remove the static declaration.
In the code inside main method, Instead of
String re = re + r;
try this,
string re = "";
re+=r;
Upvotes: 0
Reputation: 7591
In your code error: variable re might not have been initialized because it will only initialized if for loop condition is true
Other thing is you try to declare String re twice as a static variable outside the method and also inside the method
Not only that, In your code you try to access variable out side it's scope.
class Demo{
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);
re = re + r ;
}
System.out.println(re);
}
}
Read
http://javaseeeedu.blogspot.com/2016/01/local-variable-global-variable-instance.html
Upvotes: 1
Reputation: 726549
The problem is that you are re-declaring your re
variable as a local variable, "shadowing" the static String re
field declared outside of main
.
In general, it is not a good idea to make mutable static
fields, so you should move the declaration of String re = ""
inside main()
, and replace String re = re + r ;
declaration with re += r
.
Note: Although the above will get your code to work, it is not a good idea to append to a String
variable inside a loop. You would be better off using a StringBuilder
object, and calling its append
method inside the loop:
StringBuilder re = new StringBuilder();
for (...) {
...
re.append(r);
}
Upvotes: 2
Reputation: 218837
You declare a variable at the class level:
static String re = "";
But then you re-declare it in a local scope:
String re = re + r;
So this line of code (as well as anything else in this scope) is going to try to use this variable instead of the class-level one. In this line of code you reference the variable twice, and that second reference is attempting to get a value from the variable.
But this same line is also declaring and setting the initial value to the variable. You can't get a value before it's ever been set. Thus the error.
If you want to use the class-level variable, remove the re-declaration:
re = re + r;
Upvotes: 2