Colise Sanders
Colise Sanders

Reputation: 11

Java label and break error

Here's the code after Joachim's solution:

http://codepad.org/AB2gu59t

Here are the errors I get now:

Scripts\OHDHerblaw.java:109: error: undefined label: label1084 if (getInventoryCount(new int[] { this.Herb }) == 0) if (getInventoryCou nt(new int[] { this.Unfinished }) == 0) if (getInventoryCount(new int[] { this.I dentified }) == 0) break label417; if (getInventoryCount(new int[] { this.Vials }) != 0) if (getInventoryCount(new int[] { this.Identified }) != 0) { if (getIn ventoryCount(new int[] { this.Vials }) != 0) if (getInventoryCount(new int[] { t his.Identified }) != 0) break label1084; if (getInventoryCount(new int[] { this. Unfinished }) != 0)

Scripts\OHDHerblaw.java:110: error: undefined label: label1084 break label1084; }

Scripts\OHDHerblaw.java:201: error: undefined label: label1391 break label1391;

Scripts\OHDHerblaw.java:202: error: undefined label: label1706 if (getInventoryCount(new int[] { this.Vials }) != 0) if (getInventoryCo unt(new int[] { this.Identified }) != 0) { if (getInventoryCount(new int[] { thi s.Vials }) != 0) if (getInventoryCount(new int[] { this.Identified }) != 0) brea k label1706; if (getInventoryCount(new int[] { this.Unfinished }) != 0)

Scripts\OHDHerblaw.java:203: error: undefined label: label1706 break label1706; }

5 errors

I'm not sure how to define the labels.

Well after some struggle, I got it to work. Here's the modified code if anyone's interested.

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class OHDHerblaw extends Script {

   int Herb;
   int Identified = 0;
   int Unfinished;
   int Ingrediant;
   int potsMade;
   int Vials = 464;
   int[] POTARRAY = new int[]{222, 474, 477, 480, 483, 486, 489, 492, 495, 498, 566, 569, 570};
   int[] UNIDARRAY = new int[0];
   int[] IDARRAY = new int[0];
   boolean onlyId = false;
   boolean idHerbs = false;
   boolean isMixing = false;
   boolean unfinishedOnly = false;


   public OHDHerblaw(Extension var1) {
      super(var1);
   }

   public void init(String var1) {
      String[] var2 = var1.trim().replaceAll(" ", "").split(",");
      if(!var2[0].equals("")) {
         this.Herb = Integer.parseInt(var2[0]);
         if(this.Herb <= 443) {
            if(this.Herb == 165 || this.Herb >= 435 && this.Herb <= 443) {
               this.idHerbs = true;
               this.unfinishedOnly = false;
               if(this.Herb == 165) {
                  this.Identified = 444;
                  this.Unfinished = 454;
               } else if(this.Herb > 165) {
                  this.Identified = this.Herb + 10;
                  this.Unfinished = this.Herb + 20;
               }
            }
         } else if(this.Herb >= 444) {
            if(this.Herb >= 444 && this.Herb <= 453) {
               this.Herb -= 10;
               this.Identified = this.Herb + 10;
               this.Unfinished = this.Herb + 20;
               this.idHerbs = false;
               this.unfinishedOnly = false;
            } else if(this.Herb >= 454 && this.Herb <= 463) {
               this.Herb -= 20;
               this.Identified = this.Herb + 10;
               this.Unfinished = this.Herb + 20;
               this.idHerbs = false;
               this.unfinishedOnly = true;
            }
         } else {
            System.out.println("That\'s is not a valid Herb Id...");
            this.stopScript();
         }
      }

      if(!var2[1].equals("")) {
         this.Ingrediant = Integer.parseInt(var2[1]);
         if(this.Ingrediant != 270 && this.Ingrediant != 473 && this.Ingrediant != 220 && this.Ingrediant != 219 && this.Ingrediant != 471 && this.Ingrediant != 469 && this.Ingrediant != 472 && this.Ingrediant != 501) {
            System.out.println("No ingredient selected. We will not be making full potions...");
            this.Ingrediant = 0;
            this.isMixing = false;
         } else {
            this.isMixing = true;
         }
      }

      if(this.unfinishedOnly && !this.isMixing) {
         System.out.println("Not possible, please re-check script setup...");
         this.stopScript();
      }

   }

   public int main() {

      if(this.getFatigue() >= 90) {
         this.useSleepingBag();
         return this.random(800, 1000);
      } else {
         if(!this.onlyId) {
            int[] var1;
            if(this.isMixing) {
               if(this.idHerbs && this.getInventoryCount(new int[]{this.Herb}) > 0) {
                  this.useItem(this.getInventoryIndex(new int[]{this.Herb}));
                  return this.random(200, 350);
               }

               if(this.getInventoryCount(new int[]{this.Identified}) > 0 && this.getInventoryCount(new int[]{this.Vials}) > 0 && this.getInventoryCount(new int[]{this.Herb}) == 0) {
                  this.useItemWithItem(this.getInventoryIndex(new int[]{this.Identified}), this.getInventoryIndex(new int[]{this.Vials}));
                  return this.random(200, 350);
               }

               if(this.getInventoryCount(new int[]{this.Ingrediant}) > 0 && this.getInventoryCount(new int[]{this.Unfinished}) > 0) {
                  this.useItemWithItem(this.getInventoryIndex(new int[]{this.Ingrediant}), this.getInventoryIndex(new int[]{this.Unfinished}));
                  return this.random(500, 600);
               }

               if(this.getInventoryCount(new int[]{this.Herb}) == 0 && this.getInventoryCount(new int[]{this.Unfinished}) == 0 && this.getInventoryCount(new int[]{this.Identified}) == 0 || this.getInventoryCount(new int[]{this.Vials}) == 0 || this.getInventoryCount(new int[]{this.Identified}) == 0 || (this.getInventoryCount(new int[]{this.Vials}) == 0 || this.getInventoryCount(new int[]{this.Identified}) == 0) && this.getInventoryCount(new int[]{this.Unfinished}) == 0) {
                  if(this.isQuestMenu()) {
                     this.answer(0);
                     return this.random(2000, 3000);
                  }

                  if(this.isBanking()) {
                     int var2;
                     if(this.unfinishedOnly) {
                        if(this.getInventoryCount(new int[]{this.Ingrediant}) == 0 || this.getInventoryCount(new int[]{this.Unfinished}) == 0) {
                           for(var2 = 0; var2 < this.POTARRAY.length; ++var2) {
                              if(this.getInventoryCount(new int[]{this.POTARRAY[var2]}) > 0) {
                                 this.deposit(this.POTARRAY[var2], this.getInventoryCount(new int[]{this.POTARRAY[var2]}));
                                 return this.random(1250, 1500);
                              }
                           }
                        }

                        if(this.getInventoryCount(new int[]{this.Unfinished}) == 0) {
                           this.withdraw(this.Unfinished, 14);
                           return this.random(1500, 2000);
                        }

                        if(this.getInventoryCount(new int[]{this.Ingrediant}) == 0 && this.getInventoryCount(new int[]{this.Unfinished}) > 0) {
                           this.withdraw(this.Identified, this.getInventoryCount(new int[]{this.Unfinished}));
                           return this.random(1500, 2000);
                        }

                        this.closeBank();
                        return this.random(200, 300);
                     }

                     if(!this.unfinishedOnly) {
                        if(this.getInventoryCount(new int[]{this.Ingrediant}) == 0 || this.getInventoryCount(new int[]{this.Unfinished}) == 0) {
                           for(var2 = 0; var2 < this.POTARRAY.length; ++var2) {
                              if(this.getInventoryCount(new int[]{this.POTARRAY[var2]}) > 0) {
                                 this.deposit(this.POTARRAY[var2], this.getInventoryCount(new int[]{this.POTARRAY[var2]}));
                                 return this.random(1250, 1500);
                              }
                           }
                        }

                        if(this.getInventoryCount(new int[]{this.Unfinished}) > 0) {
                           this.withdraw(this.Ingrediant, this.getInventoryCount(new int[]{this.Unfinished}));
                           this.closeBank();
                           return this.random(2000, 3000);
                        }

                        if(this.getInventoryCount(new int[]{this.Vials}) == 0) {
                           this.withdraw(this.Vials, 14);
                           return this.random(1000, 1500);
                        }

                        if(this.getInventoryCount(new int[]{this.Herb}) == 0 && this.getInventoryCount(new int[]{this.Unfinished}) == 0 && this.getInventoryCount(new int[]{this.Identified}) == 0 && this.idHerbs) {
                           this.withdraw(this.Herb, 14);
                           return this.random(2000, 3000);
                        }

                        if(this.getInventoryCount(new int[]{this.Identified}) == 0) {
                           this.withdraw(this.Identified, 14);
                           return this.random(1000, 1500);
                        }

                        this.closeBank();
                        return this.random(1000, 1500);
                     }
                  }

                  var1 = this.getNpcByIdNotTalk(this.BANKERS);
                  if(var1[0] != -1 && !this.isBanking() && !this.isQuestMenu() && this.getInventoryCount(new int[]{this.Ingrediant}) == 0) {
                     this.talkToNpc(var1[0]);
                     return this.random(3250, 3500);
                  }
               }
            }

            if(!this.isMixing) {
               if(this.idHerbs && this.getInventoryCount(new int[]{this.Herb}) > 0) {
                  this.useItem(this.getInventoryIndex(new int[]{this.Herb}));
                  return this.random(200, 350);
               }

               if(this.getInventoryCount(new int[]{this.Identified}) > 0 && this.getInventoryCount(new int[]{this.Vials}) > 0 && this.getInventoryCount(new int[]{this.Herb}) == 0) {
                  this.useItemWithItem(this.getInventoryIndex(new int[]{this.Identified}), this.getInventoryIndex(new int[]{this.Vials}));
                  return this.random(200, 350);
               }

               if(this.getInventoryCount(new int[]{this.Herb}) == 0 && this.getInventoryCount(new int[]{this.Unfinished}) == 0 && this.getInventoryCount(new int[]{this.Vials}) == 0 && this.getInventoryCount(new int[]{this.Identified}) == 0 || this.getInventoryCount(new int[]{this.Vials}) == 0 || this.getInventoryCount(new int[]{this.Identified}) == 0 || (this.getInventoryCount(new int[]{this.Vials}) == 0 || this.getInventoryCount(new int[]{this.Identified}) == 0) && this.getInventoryCount(new int[]{this.Unfinished}) == 0) {
                  if(this.isQuestMenu()) {
                     this.answer(0);
                     return this.random(2000, 3000);
                  }

                  if(this.isBanking()) {
                     if(this.getInventoryCount(new int[]{this.Herb}) == 0 && this.getInventoryCount(new int[]{this.Unfinished}) == 0 && this.getInventoryCount(new int[]{this.Vials}) == 0 && this.getInventoryCount(new int[]{this.Identified}) == 0 && this.idHerbs) {
                        this.withdraw(this.Herb, 14);
                        return this.random(2000, 3000);
                     }

                     if(this.getInventoryCount(new int[]{this.Unfinished}) > 0) {
                        this.deposit(this.Unfinished, this.getInventoryCount(new int[]{this.Unfinished}));
                        return this.random(200, 300);
                     }

                     if(this.getInventoryCount(new int[]{this.Vials}) == 0) {
                        this.withdraw(this.Vials, 14);
                        return this.random(1000, 1500);
                     }

                     if(this.getInventoryCount(new int[]{this.Identified}) == 0) {
                        this.withdraw(this.Identified, 14);
                        return this.random(1000, 1500);
                     }

                     this.closeBank();
                     return this.random(1000, 1500);
                  }

                  var1 = this.getNpcByIdNotTalk(this.BANKERS);
                  if(var1[0] != -1 && !this.isBanking() && !this.isQuestMenu()) {
                     this.talkToNpc(var1[0]);
                     return this.random(3500, 3750);
                  }
               }
            }
         }

         return this.random(200, 300);
      }
   }
}

Upvotes: 0

Views: 1796

Answers (4)

Stephen C
Stephen C

Reputation: 718906

@Joachim's answer has nailed it.

I just want to explain WHY ...

A break <label> statement can only break out to an enclosing statement in the current method that has the given label. A local variable declaration cannot include a break statement. Therefore a label on a local variable declaration cannot be broken too.

I suspect that you were trying to use the break <label> statement like a C / C++ goto statement. It doesn't work that way. Even if a label was allowed there, you wouldn't be able to break to it. For instance, this is equally wrong:

label: if (i == 1) {
   // so something
}
if (i == 2) {
   break label;  // compilation error
}

Upvotes: 1

Joachim Sauer
Joachim Sauer

Reputation: 308061

A label can only be used before a statement (JLS §14.7).

A local variable declaration is not a statement (in JLS-speak it's a "block statement" (JLS §14.2), which is a super-set of statement).

Simple move your int[] arrayOfInt; to the line before the label and you should be good:

int[] arrayOfInt;
label417:
if (this.isMixing) {
  ...

Upvotes: 2

Jesper
Jesper

Reputation: 206846

A label must be followed by a statement. In your code, it's followed by a variable declaration, which is not a statement.

Move the label after the variable declaration:

int[] arrayOfInt;

label417:
if (this.isMixing) {
    // ...

By the way, I regard using labels in Java code as a code smell - something that indicates that there's something wrong with the design of the code. A label is like a goto statement, which is an infamous construction that makes code hard to understand (it leads to spaghetti code).

Instead of using labels, use other control structures such as for or while loops.

Upvotes: 1

Viral Patel
Viral Patel

Reputation: 8601

Label on assignment has a bit different syntax:

 int i;
 label12: i = 42; // label on the assignment

If you are assigning value to a label you have to define it first.

In your case, change the code to:

if (!this.onlyId)
    {
      int[] arrayOfInt;
      label417:      
      if (this.isMixing)
      {
        if (this.idHerbs) {
          if (getInventoryCount(new int[] { this.Herb }) > 0) {
            useItem(getInventoryIndex(new int[] { this.Herb }));
            return random(200, 350);
          }
        }

Upvotes: 0

Related Questions