Java incompatible types: int cannot be converted to int[]

She
class Solution {
    public int[] Test(int[] x, int target) {
        target = 100;
        int i;
        int j;
        int sum;
        for (i = 0; i > 3; i++) {
            for (j = 0; j > 3; j++) {
            sum = x[i] + x[j];
                if (sum == target) {
                    return x[i];
                    return x[j];
                }
            }
        }
    }
}

I am trying to write a code to return 2 elements (from an array) which give me the target integer 100 when summed. I keep getting the following 2 errors:

incompatible types: int cannot be converted to int[] 
                    return x[i];                         
incompatible types: int cannot be converted to int[] 
                    return x[j];
Andrew Tobilko
return new int[]{x[i], x[j]};

You want to return an array int[], not a single int value.

return x[i];
return x[j];

doesn't make any sense because a return statement immediately interrupts the flow (returns control to the invoker) making the following statements unreachable.

You are also missing a return statement at the end. When target hasn't been met, you still have to return something from the method.

It could be an empty array:

return new int[0];

However, usually, we throw an exception saying the given arguments didn't make the method work:

throw new IllegalArgumentException("The target wasn't met for the given input.");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java incompatible types: int cannot be converted to int[]

From Java

Java : incompatible types; int cannot be converted to string

From Dev

error: incompatible types: int[][] cannot be converted to int

From Dev

JAVA: Incompatible types: int cannot be converted to java.lang.String

From Dev

Java: toString error "error: incompatible types: int cannot be converted to String"

From Java

incompatible types: int cannot be converted to T

From Dev

incompatible types: void cannot be converted to int

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

incompatible types - int cannot be converted in to java.lang.string -java - bluej

From Dev

app setBackground() error: incompatible types: int cannot be converted to Drawable

From Dev

Incompatible types : Int cannot be converted to "class" data type

From Dev

Java - Getting compile error incompatible types int can't be converted to [][]

From Dev

Java JComboBox Incompatible Types: Cannot be converted to string

From Dev

Java int cannot be converted to boolean

From Dev

int[][] cannot be converted into int

From Dev

int cannot be converted to int []

From Dev

Incompatible operand types between K and int Java

From Dev

JAVA incompatible types: Object cannot be converted to my type

From Dev

java.lang.Object cannot be converted to int

From Dev

double[][] cannot be converted to int[][]

From Dev

incompatible types: Node cannot be converted to Tab

From Dev

incompatible types: HomeFragment cannot be converted to Fragment in Android

From Dev

Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

From Dev

Error: incompatible types: double cannot be converted to JTextField

From Dev

Error: incompatible types: Object cannot be converted to char

From Dev

Incompatible types: Object cannot be converted to ParseObject

From Dev

incompatible types: char[] cannot be converted to CharSequence

From Dev

error: incompatible types: MainFragment cannot be converted to Activity

Related Related

  1. 1

    Java incompatible types: int cannot be converted to int[]

  2. 2

    Java : incompatible types; int cannot be converted to string

  3. 3

    error: incompatible types: int[][] cannot be converted to int

  4. 4

    JAVA: Incompatible types: int cannot be converted to java.lang.String

  5. 5

    Java: toString error "error: incompatible types: int cannot be converted to String"

  6. 6

    incompatible types: int cannot be converted to T

  7. 7

    incompatible types: void cannot be converted to int

  8. 8

    incompatible types: BigInteger cannot be converted to int

  9. 9

    incompatible types: BigInteger cannot be converted to int

  10. 10

    incompatible types - int cannot be converted in to java.lang.string -java - bluej

  11. 11

    app setBackground() error: incompatible types: int cannot be converted to Drawable

  12. 12

    Incompatible types : Int cannot be converted to "class" data type

  13. 13

    Java - Getting compile error incompatible types int can't be converted to [][]

  14. 14

    Java JComboBox Incompatible Types: Cannot be converted to string

  15. 15

    Java int cannot be converted to boolean

  16. 16

    int[][] cannot be converted into int

  17. 17

    int cannot be converted to int []

  18. 18

    Incompatible operand types between K and int Java

  19. 19

    JAVA incompatible types: Object cannot be converted to my type

  20. 20

    java.lang.Object cannot be converted to int

  21. 21

    double[][] cannot be converted to int[][]

  22. 22

    incompatible types: Node cannot be converted to Tab

  23. 23

    incompatible types: HomeFragment cannot be converted to Fragment in Android

  24. 24

    Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

  25. 25

    Error: incompatible types: double cannot be converted to JTextField

  26. 26

    Error: incompatible types: Object cannot be converted to char

  27. 27

    Incompatible types: Object cannot be converted to ParseObject

  28. 28

    incompatible types: char[] cannot be converted to CharSequence

  29. 29

    error: incompatible types: MainFragment cannot be converted to Activity

HotTag

Archive