0

I need to write a method that when passed a double array arr, and an int value target, returns the index of the first occurence of an item whose integer part equals target.

for example if arr = {1.2, 3.6, 5.2, 9.2, 5.6}, and target = 5, the target must return 2 and return -1 if array is null.

My code so far is

public static int yes(double[]arr, int target){
    if (arr == null){
        return -1;
    }

    for (int i= 0; i < arr.length(); i++){
        if(arr[i] == target)
            return i;
    } 

    return 1;
}

and theres a compile error, I just dont understand converting between double and int? Thanks

2
  • Double is a decimal number (simplified, anyways). Integer is a whole number, rounded down (truncated). Comparing is 5.2 is the same as 5 will not return true. You can cast the double to an int, by doing this: (int) arr[i] Commented Nov 12, 2016 at 6:16
  • 1
    Although this code is pretty trivial, next time you are looking for help with a compiler error you should provide (a) the error message and (b) the line it occurs at. Commented Nov 12, 2016 at 6:20

6 Answers 6

2

Try casting your arr[i] to an int when checking to see if it is equal to your target:

for (int i= 0; i < arr.length; i++){
            if((int)arr[i] == target)
                return i;
        }

Doing this will drop the decimal part of arry[i], making it integer, and thus comparable to your target.

Sign up to request clarification or add additional context in comments.

Comments

1
public class Main{
    public static void main (String[] args) {
        double[] arr = new double[]{1.2, 3.6, 5.2, 9.2, 5.6};
        int target =  31;
        System.out.println(yes(arr,target));
    }
    public static int yes(double[] arr, int target){
        if (arr == null){
            return -1;
        }

        for (int i= 0; i < arr.length; i++){
            if((arr[i] - target) >= 0.0 && (arr[i] - target) < 1.0)
                return i;
        }

        return -1;
    }
}

Comments

0

Your code is perfect you just missed to cast the double

(int) arr[i] == target


public static int yes(double[]arr, int target){
    if (arr == null){
        return -1;
    }

    for (int i= 0; i < arr.length(); i++){
        if((int) arr[i] == target)
            return i;
    } 

    return 1;
}

Comments

0

Use this as your for loop

for (int i= 0; i < arr.length(); i++){
        if((int)arr[i] == target)
             return i;
    }

Upvote if it helps!

Comments

0

The code below should work. It casts the double that is being compared against target to an int. So the example double array: {1.2, 3.6, 5.2, 9.2, 5.6} would become {1, 3, 5, 9, 5} and when compared with the target you will get the correct return. I do suggest that you make the function return -1 if you don't find the target in your array. Good luck, happy coding!

public static int yes(double[]arr, int target){
    if (arr == null){
        return -1;
    }

    for (int i= 0; i < arr.length; i++){
        if((int)arr[i] == target) return i;
        //use (int) to cast the double to an int. 3.9 and 3.2 would both become an int of 3
    } 

    return 1;
    //returns 1 if target is not in arr
}

Comments

0
for (int i = 0; i < arr.length(); i++) {
    if ((int) arr[i] == target)
        return i;
}

This is OK, but I thought this maybe better.

for (int i = 0; i < arr.length(); i++) {
    if (arr[i] == (double)target)
        return i;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.