0

enter image description here

enter image description here

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614

4 Answers4

3

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?

Community
  • 1
  • 1
Colin D
  • 5,641
  • 1
  • 23
  • 35
1

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Yes i know goto isnt a statment in java but is there any other statment which is like it? – user1800504 Nov 05 '12 at 15:03
  • 1
    No. Although you can fake some of the functionality of a goto with labeled breaks or even with exception throwing, but by all means, don't. Something is awfully wrong if you think that you _have to_ use such an statement in Java – Óscar López Nov 05 '12 at 15:05
  • Okay thank you but would you have any idea on how i could fix the problem? like re-write it? – user1800504 Nov 05 '12 at 15:08
  • Work with the original source code, fiddling with a non-compiling generated piece of code is a waste of time, it'll take you more time to understand and fix it than to write it from scratch. – Óscar López Nov 05 '12 at 15:11
0

Instead of goto you can use lable and break

Ex- :

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

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

     }
someone
  • 6,577
  • 7
  • 37
  • 60
0

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.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75