3 Replies Latest reply: Oct 9, 2009 10:17 AM by 843810 RSS

    I need help with this code error "unreachable statement"

    843810
      the error_


      F:\Java\Projects\Tools.java:51: unreachable statement <-----------------------------------------------------------------------------------------------------------------THIS
      int index;
      ^
      F:\Java\Projects\Tools.java:71: missing return statement
      }//end delete method
      ^
      F:\Java\Projects\Tools.java:86: missing return statement
      }//end getrecod
      ^
      3 errors


      import java.util.*;
      import javax.swing.*;
      import java.awt.*;


      public class Tools//tool class
      {
      private int numberOfToolItems;
      private ToolItems[] toolArray = new ToolItems[10];



      public Tools()//array of tool
      {
      numberOfToolItems = 0;
      for(int i = 0; i < toolArray.length; i++)//for loop to create the array tools
      {
      toolArray[i] = new ToolItems();
      }//end for loop

      }//end of array of tools

      public int search(int id)//search mehtod
      {

      int index = 0;
      while (index < numberOfToolItems)//while and if loop search
      {
      if(toolArray[index].getID() == id)

      return index;
      else
      index ++;



      }//en while and if loop
      return -1;
      }//end search method

      public int insert(int id, int numberInStock, int quality, double basePrice, String nm)//insert method
      {
      if(numberOfToolItems >= toolArray.length)
      {
      return 0;
      int index;
      index = search(id); <-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------HERE

      if (index == -1)
      {
      toolArray[index].assign(id,numberInStock, quality, basePrice,nm);
      numberInStock ++;

      return 1;


      }//end if index
      }//end if toolitem array

      return -1;
      }//end insert method

      public int delete(/*int id*/)//delete method
      {

      }//end delete method

      public void display()//display method
      {
      for(int i = 0; i < numberOfToolItems; i++)
      {
      //toolArray.display(g,y,x);
      }

      }//end display method


      public String getRecord(int i)//get record method
      {
      // return toolArray[i].getName()+ "ID: "+toolArray[i].getID()
      }//end getrecod

      }//end class

      Edited by: ladsoftware on Oct 9, 2009 6:08 AM

      Edited by: ladsoftware on Oct 9, 2009 6:09 AM

      Edited by: ladsoftware on Oct 9, 2009 6:10 AM

      Edited by: ladsoftware on Oct 9, 2009 6:11 AM
        • 1. Re: I need help with this code error "unreachable statement"
          800560
          When you post code, click on the "CODE" label right above the text entry box and make sure your code is inside the code tags. Click the Preview tab to make sure it looks correct. The forum software will usually make it very difficult for people to analyze your code if you do not use the code tags.

          So here's part of your code, formatted with code tags.
          if(numberOfToolItems >= toolArray.length) {
              return 0;
              int index;  // The error message makes it appear that this is line 51
              index = search(id); 
              if (index == -1) {
                  toolArray[index].assign(id,numberInStock, quality, basePrice,nm);
                  numberInStock ;
          
                  return 1;
              }
          }
          Do you see a problem here? If you do not see a problem, then I can only assume you do not know what a return statement does. The unreachable statement error is quite simple to understand. The compiler is telling you that you have code which will never be executed, but you wrote it as if you expected it to be executed. The compiler will not let you get away with that. You either do not want to return before those statements execute, or you do not need those statements where they currently appear.

          The other two errors mean what they say. You wrote code that said a method would return some value. But inside that method there is no return of any value. The compiler will not let you get away with that either.
          • 2. Re: I need help with this code error "unreachable statement"
            843810
            thanks
            i'm new here and new to Java
            • 3. Re: I need help with this code error "unreachable statement"
              843810
              ladsoftware wrote:
              Subject: Re: I need help with this code error "unreachable statement"

              F:\Java\Projects\Tools.java:51: unreachable statement <-----------------------------------------------------------------------------------------------------------------THIS
              int index;
              ^
              F:\Java\Projects\Tools.java:71: missing return statement
              }//end delete method
              ^
              F:\Java\Projects\Tools.java:86: missing return statement
              }//end getrecod
              ^
              3 errors
              The compiler is telling you exactly what the problems are:
              public int insert(int id, int numberInStock, int quality, double basePrice, String nm)//insert method
              {
              if(numberOfToolItems >= toolArray.length)
              {
              return 0; // <<== HERE you return, so everyting in the if block after this is unreachable
              int index;
              index = search(id);  //< -----------------------------------------------------------------------------------------------------------------HERE
              
              if (index == -1)
              {
              toolArray[index].assign(id,numberInStock, quality, basePrice,nm);
              numberInStock ++;
              return 1;
              }//end if index
              }//end if toolitem array
              return -1;
              }//end insert method
              
              public int delete(/*int id*/)//delete method
              {
              // <<== HERE where is the return statement?
              }//end delete method
              
              public String getRecord(int i)//get record method
              {
              // return toolArray.getName()+ "ID: "+toolArray[i].getID() <<== HERE you commented out the return statement
              }//end getrecod

              }//end class