Sean
Sean

Reputation: 7

incompatible types: java.lang.String cannot be converted to int[]

I am making a java program that prints the even and odd numbers of an array. This is my even code that returns the evens variable and prints the evens in the runner case.

public static int[] getAllEvens(int[] array)
    {  
        String evens;

        for(int i=0; i<array.length; i++)
        {
            if(array[i]%2==0)
            {
                 evens = (array[i]+" ");
            }
        }
        return evens;   
    }

But when I compile, it says that the evens variable cannot be converted to int[]. I get that String or int shouldn't be used to define evens but I do not know how else I can return array[i]+" ".This is so difficult for me.

Upvotes: 0

Views: 10676

Answers (3)

some user
some user

Reputation: 1775

Edit 1: Using Scanner & using temporary ArrayList to make int[] compact

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int[] evens = getAllEvens1(arr);
        for (int i : evens) {
            System.out.print(i + " ");
        }
    }

    public static int[] getAllEvens1(int[] array) {
        int[] temp = new int[array.length]; // if all of array are `even`..
        Arrays.fill(temp, -1); //or any flag.. to indicate it's unnecessary

        int j = 0;
        for (int i : array) {
            if (i % 2 == 0) {
                temp[j++] = i;
            }
        }

        int[] ret = new int[j];
        j = 0;
        for (int i : temp) {
            if (i != -1) {
                ret[j++] = i;
            }
        }
        return ret;

    }

    public static int[] getAllEvens(int[] array) {
        ArrayList<Integer> temp = new ArrayList();

        for (int i : array) {
            if (i % 2 == 0) {
                temp.add(i);
            }
        }
        return temp.stream().mapToInt(Integer::intValue).toArray();
    }
}

There are several problems in your code.. First you're trying to return String whereas your function return type is int[] Secondly, you're assigning only current even number to the String which hasn't even initialized yet..

Fix:

 public static String getAllEvens(int[] array) {
        StringBuilder evens = new StringBuilder();

        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 == 0) {
                evens.append(array[i]).append(" ");
            }
        }
        return evens.toString();
    }

Or by using String instead of StringBuilder:

  public static String getAllEvens(int[] array) {
        String ret = "";

        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 == 0) {
                ret += array[i] + " ";
            }
        }
        return ret;
    }

Upvotes: 1

Brooklyn99
Brooklyn99

Reputation: 1047

you could do some thing like this:

FYI: I have not tested this but should be ok.

public static int[] getAllEvens(int[] array) {

int[] tmp = new int[array.length];
   for (i = 0; i < array.length; i++) {
       if (array[i] % 2 == 0) {
          tmp[even_count++] = array[i];
       }
   }

return tmp;
}

Upvotes: 0

Ranjith Bokkala
Ranjith Bokkala

Reputation: 379

please change the return type of the method. your returning String but method return type is int Array. That is the issue you're unable to compile the code.

Upvotes: 0

Related Questions