There is no need to reinvent the wheel; compilers are already doing it for you. The first step in any compilation process is to check if the markers in the file are within the language. This, of course, will not help us, since English and java are not different from each other. However, the second step, parsing, will throw an error with any sentence written in English instead of java code (or something else that is not proper java). So, instead of using external libraries and try using an alternative approach, why don't you use the already available java compiler?
you can have a wrapper class like
public class Test{ public static void main(){ } }
which compiles, and if it goes well, then bum, you know its valid code. Of course, it will not work with code fragments that are not complete, for example, for a loop that you put in an example without an end bracket. If it does not compile, you can threaten this line with ways, for example, try to parse it with your own pseudo-random English parser, made using a flexible bison, the GNU tools used to create GCC, for example. I don’t know exactly what you are trying to accomplish using the program you are trying to do, but in this way you can know if it is encoded, manual English, or just garbage that you don't care. The analysis of natural languages is very difficult, and at the moment, modern approaches use inaccurate statistical methods, so they are not always right, which you may not need in your program.
source share