0

I'm working on a java program, but I'm stuck on this specific section. This function is suppose to take an array as input and returns the index of the first occurrence of target in the input array, or -1 if not found. The function is suppose to call my contains method.

contains()

public static boolean contains(int[] input, int target) {
        for(int i = 0; i < input.length; i++){
            if (target == input[i]){

                return true;
            }
        }
        return false;
    }

indexof()

public static int indexOf(int[] input, int target) {
        if(contains(input, target) == true){
            return i;
        }
        return -1;
    }

I am trying to return what the variable i was in the contains method, but I'm not sure how to make the variable i transfer from the contains method to the indexof method without making it a public int in the constructor. Thanks for your help.

----------EDIT---------- The contains method is suppose to search the given array for the target value. If the target value exists somewhere in the array, return true. if not, return false. If any alterations need to be made to the contains method, that may be done as well.

Shan Solo
  • 57
  • 2
  • 10

4 Answers4

1

indexOf should look a lot like contains. In fact, they should kind of be switched from what you have:

public static int indexOf(int[] input, int target) {
        for(int i = 0; i < input.length; i++){
            if (target == input[i]){
                return i;
            }
        }
        return -1;
    }

public static boolean contains(int[] input, int target) {
        if(indexOf(input, target) >= 0){
            return true;
        }
        return false;
    }
djhaskin987
  • 9,741
  • 4
  • 50
  • 86
1

There is a good chance you have this backwards. Either your instructor has a typo in his homework, or you misread or misunderstood.

It makes more sense for the contains() method to use the indexOf() method. This way should be quite easy to figure out.

  • "Build this indexOf() function by calling contains, which you’ve already made above." I already emailed him, he didn't make a typo. – Shan Solo Mar 07 '14 at 03:43
1

The only thing that would make sense would be to perform a check in your indexOf() using contains(), to make sure the variable exists.

public static boolean contains(int[] input, int target) {
    for(int i = 0; i < input.length; i++){
        if (target == input[i]){
            return true;
        }
    }
    return false;
}

public static int indexOf(int[] input, int target) {

    //If the value isn't there, return -1
    if(contains(input, target) == false){
        return -1;
    }

    //Go find the value if we know it is in there
    for(int i = 0; i < input.length; i++){
        if (target == input[i]){
            return i;
        }
    } 
    //We should never get here
    return -1;   
}
Tyler
  • 17,669
  • 10
  • 51
  • 89
  • This is the only one that makes sense right now. The instructor didn't say how we had to use the contains method, so I'm guessing this would be fine. Thank you! – Shan Solo Mar 07 '14 at 04:20
  • You're welcome. And you're right, it doesn't make that much sense to loop through the array twice. – Tyler Mar 07 '14 at 04:21
0

Another way to do is by setting a static variable equals to index of target. The code is shown below :

import java.util.*;
import java.lang.*;
import java.io.*;


class TargetSearch
{
private static int posOfTarget;
public static void main (String[] args) throws java.lang.Exception
{
    int [] arr = {1,2,3,4,5,6};
    int target = 5;
    if(contains(arr,target)){
        System.out.println(target+" found at pos " + getPosOfTarget()+1);
    }
    else
        System.out.println(target+" doesn't exist in arr ");
}

public static boolean contains(int[] input, int target) {
for(int i = 0; i < input.length; i++){
    if (target == input[i]){
        setPosofTarget(i);
        return true;
    }
}
return false;
}

public static int indexOf(int[] input, int target) {
    if(contains(input, target) == true){
        //pos is already set in contains()
        return (getPosOfTarget());
    }
    else
        return -1;   
}

//getter setter of pos
public static int getPosOfTarget(){
    return posOfTarget;
};
public static void setPosofTarget(int i){
    posOfTarget = i;
};

}

KNU
  • 2,560
  • 5
  • 26
  • 39