Errors compiling a .java? (goto) [closed]

耗尽温柔 提交于 2019-12-11 10:45:59

问题


I got this .java and i want to compile it to a .class. The only thing is that the guy who actually made this used very sloppy code. I got 14 errors and they're all the same. There are 3 lines that have all the errors. They all have the same errors and the only thing is I don't know how to fix them.

Code with error example:

if(!i$.hasNext()) 
    goto L2;
else 
    goto L1;

The error has iS at the goto and is saying illegal start of expression. That's the first error.

  • Another is where the L2 is and is saying not a statement.
  • And again at the else it says 'else without 'if'.
  • And again with the goto illegal start of expression.

I also know that goto isn't even a statement, its reserved and isn't used, partly because it is not used in java! But if there is anyone out there who knows a statement other than goto which will link back to a label please tell, I will be much grateful!

Update: May I point out that I did not make this.


回答1:


goto is not implemented in java. it is a reserved keyword but does not do anything.

This question has been asked before:

Is there a goto statement in Java?




回答2:


Java doesn't have a goto statement. To be clear, goto is a reserved keyword in Java (see section §3.9 of the Java Language Specification), but it has no use whatsoever. The snippet of code you provided won't even compile.

By the looks of it, it appears that you're looking at some intermediate representation of code, or an obfuscated/decompiled source (because of the goto and $ variable identifiers). Make sure to take a look at the original .java source code file.




回答3:


Instead of goto you can use lable and break

Ex- :

 mylable:  
  for (i = 0; i < 10; i++) {

            if (somecondition) {
               //doSomething()
               break mylable;
          }

     }



回答4:


This looks as though it may be a failed attempt to translate a state machine implementation into Java from a language with goto.

There are several ways of constructing a state machine in Java. Perhaps the simplest is a switch statement inside a loop.

while(!done) {
  switch(state) {
    case 1:
       ...
       break;
    case 4:
       if(!input.hasNext()) state = 2; else state = 1;
       break;
    case 5:
       ...
       break;
  }
}

There are alternatives in which you represent the current state as an object implementing an interface, with methods you can call to do the work and get the object representing the next state.

Almost certainly this code was auto-generated, not written by a human, by decompilation or otherwise. Use of "$" in identifiers is rare in human-written code. It actually looks rather like code I've seen in compilers that was auto-generated from a formal grammar.

I agree with the previous advice to try to work with the real source, if you can.



来源:https://stackoverflow.com/questions/13234595/errors-compiling-a-java-goto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!