Home > Mobile >  error: variable myS is already defined in method main(String[]) String myS = myS myString;
error: variable myS is already defined in method main(String[]) String myS = myS myString;

Time:10-11

public class Main {

    public static void main(String[] args) {
        char memoryArray[] = {'a','b','c','d','e','f'};
        int s = 1;
        int l = 3;
        String myS = "";
        for (int i = s; i < s   l; i  ) {
            char tmp = memoryArray[i];
            String myString = Character.toString(tmp);
            String myS = myS   myString;
        }
    }
}

hey so I tried doing everything, the gist of my code is to transform these characters into a string that you can add, now the original code is much more complicated but I gave an oversimplified code to find out why this error is happening, if someone can please explain why?

CodePudding user response:

You need to change the code as shown below

String myS = myS   myString;

To

myS = myS   myString;

you cannot declare variable with same name again in same method

  • Related