1

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

4
  • 2
    You are declaring and initializing it outside the main so the one inside the main doesn't get initialized. Remove the String from there and then it will use the one outside the main which would be at the class level. Commented Dec 20, 2017 at 18:53
  • Indent your code properly if you would like people to try and read it. Commented Dec 20, 2017 at 18:54
  • 2
    just use re += r; instead of String re = re + r ; Commented Dec 20, 2017 at 18:54
  • A good practice is to call the variables within a method differently from the global ones. Commented Dec 20, 2017 at 19:33

4 Answers 4

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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;

Comments

1

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

Comments

0

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;

3 Comments

I tried this a week before asking here and I showed it to my teacher and he told me to lock it up online because I find it so difficult to understand the difference between python and java in the kind of variable and other stuff
check out this link: java67.com/2016/07/… . You might take some ideas.
In Java it's String, not string. Java is case-sensitive. Also, why do this in two lines? The same thing can be done with simply: String re = r; This also serves as a classic demonstration of changing something only to get the code to compile, but the code still won't do what the OP actually wants it to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.