This discussion is archived
5 Replies Latest reply: Nov 30, 2012 5:29 AM by 977248 RSS

Search and Array

968909 Newbie
Currently Being Moderated
In college, we need to write a program that will print the index of the value that you are searching for in an array. If not found return a -1.

int index = program.find(x,4); is the line that calls the method.

I can get it to print from the method printArray, but I am unalbe to get the code under the method find to return the correct value.

The error that I am getting is "This method must return a result of type int". Not sure what I am doing wrong. I know its with my method 'find'.

Here is my code:
public class SearchableArray {
     
     public int find(int a [],int el) {
     
          for (int z = 0; z < a.length; z++){
               
               System.out.print(a[z] + "");
               
               System.out.println("\n");
          }
     
     }
     
     public void printArray( int a []) {
          
          for (int i = 0; i < a.length ; i++ ) {
               
          
               System.out.print(a[i] + "");
               
               System.out.println("\n");
          }
     }
          
     public static void main(String[] args) {

          SearchableArray program = new SearchableArray();
          
          int x [] = {1,2,3,4,5};
          
          program.printArray(x);
          
          int index = program.find(x,4);
          
          program.printArray(x);
     }

}
Edited by: SunshineJava on Nov 29, 2012 2:01 PM
  • 1. Re: Search and Array
    DrClap Expert
    Currently Being Moderated
    SunshineJava wrote:
    I can get it to print from the method printArray, but I am unalbe to get the code under the method find to return the correct value.
    The problem isn't so much that the code fails to return the correct value, the problem is that it doesn't ever return any value at all. Somewhere in that method you should have some code like this:
    return whatsit;
    where "whatsit" is an int value. And preferably that value should be the correct value.
  • 2. Re: Search and Array
    968909 Newbie
    Currently Being Moderated
    I tried that.
          public int find(int a [],int el) {
              
              for (int i = 0; i < a.length; i--){
                   
                   return el;
              
                     } 
    And then this: Error - z cannot be resolved to a variable
            public int find(int a [],int el) {
              
              for (int i = 0; i < a.length; i--){
                   
                   return z;
    
              } 
    And also this: Error - Syntax error, insert ";" to complete BlockStatements
         This method must return a result of type int
      public class SearchableArray {
         
         public int find(int a [],int el) {
              
              for (int i = 0; i < a.length; i--){
                   
                   return int z;
    
              } 
    I have been looking at it all day and am missing something.
  • 3. Re: Search and Array
    976124 Newbie
    Currently Being Moderated
    For all scenarios, the loop will always end when i = 0.
    You can test it out by creating a test case.
    1  public int find(int a [],int el) {
    2          
    3      for (int i = 0; i < a.length; i--){
    4               
    5        return el;
    6          
    7     }
    8  }
    +======================================================================+
    | Line No. |   (int) a []    |   (int) el   |   (int) i   |  a.length
    +----------+-------------+----------+---------+------------------------+
    |    3     |   {1,2,3,4,5}   |      1       |      0      |     5
    |    5     |   {1,2,3,4,5}   |      1       |      0      |     5
    |    7     |   {1,2,3,4,5}   |      1       |      0      |     5   
    |    8     |   {1,2,3,4,5}   |      1       |      -      |     -
    +----------+-------------+----------+---------+------------------------+
    public int find(int a [],int el) {
              
         for (int i = 0; i < a.length; i--){
                   
              return z;
     
         }
    }
    You will hit an error because variable z is not being defined.
          
    public class SearchableArray {
         
         public int find(int a [],int el) {
              
              for (int i = 0; i < a.length; i--){
                   
                   return int z;
     
              }
    }
    You will hit an error because variable z is not being defined.


    NOTE:
    I think you left out the closing braces at the end of the find() function.
  • 4. Re: Search and Array
    968909 Newbie
    Currently Being Moderated
    So I thought about this alot and I think I need to you an if else loop as well.
    I somehow need to return a null value (-1) and a for will only execute whats in the statement.

    So I tried this.. and still failed.

    Same Error: This method must return a result of type int

    Thoughts anyone?
         public int find(int a [],int el) {
              
    for (int i = el; i < a.length; i--)     {     
         
         if (i <= el){
                   return 0;
         }
              else 
                   return -1;
              }
         
         }
    Edited by: SunshineJava on Nov 30, 2012 5:16 AM
  • 5. Re: Search and Array
    977248 Newbie
    Currently Being Moderated
    In the method public int find(int a [],int el),
    You need to test every value iterated through the loop, when the value of a[z] equals to "el" the method must return immediately.

    This is the complete code:

    public class SearchableArray {
         
         public int find(int a [],int el) {
              for (int z = 0; z < a.length; z++)
                   if(a[z] == el) return z;
                   
              return -1;
         }
         
         public void printArray( int a []) {
              // Simplified code
              for(int i : a)
                   System.out.println(i+"\n");
         
              /**for (int i = 0; i < a.length ; i++ ) {
                   System.out.print(a[i] + "\n");
              }*/
         }
              
         public static void main(String[] args) throws ArrayIndexOutOfBoundsException {
              SearchableArray program = new SearchableArray();
              int x [] = {1,2,3,4,5};
              program.printArray(x);
              int index = program.find(x,4);
              System.out.println("The index of "+4+" is "+index);
         }

    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points