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.

6
  • You need to use the contains() method? It seems kind of redundant. Commented Mar 7, 2014 at 3:39
  • That's what I thought, but it is necessary. Commented Mar 7, 2014 at 3:40
  • Just return the index i.e i in contains method or if not found return -1. Why do you need index of? Commented Mar 7, 2014 at 3:40
  • I need the contains method to only return if the target exists or not, and the indexOf method to return what the index of the number is. Commented Mar 7, 2014 at 3:47
  • maybe you could do this as a list. see stackoverflow.com/questions/4962361/… Commented Mar 7, 2014 at 4:00

4 Answers 4

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;   
}
Sign up to request clarification or add additional context in comments.

2 Comments

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!
You're welcome. And you're right, it doesn't make that much sense to loop through the array twice.
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;
    }

1 Comment

I have to call the contains method to the indexOf method.
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.

1 Comment

"Build this indexOf() function by calling contains, which you’ve already made above." I already emailed him, he didn't make a typo.
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;
};

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.