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 Java

incompatible types: int cannot be converted to T

From Java

Java : incompatible types; int cannot be converted to string

From Dev

incompatible types: Node cannot be converted to Tab

From Dev

java.lang.Object cannot be converted to int

From Dev

Error: incompatible types: double cannot be converted to JTextField

From Dev

Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

From Dev

incompatible types: HomeFragment cannot be converted to Fragment in Android

From Dev

Error: incompatible types: Object cannot be converted to char

From Dev

incompatible types: void cannot be converted to int

From Dev

JAVA incompatible types: Object cannot be converted to my type

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

Incompatible types: Object cannot be converted to ParseObject

From Dev

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

From Dev

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

From Dev

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

From Dev

error: incompatible types: MainFragment cannot be converted to Activity

From Dev

incompatible types: char[] cannot be converted to CharSequence

From Dev

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

From Dev

incompatible types: FragmentDark cannot be converted to Fragment in Android

From Dev

Incompatible operand types between K and int Java

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

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

From Dev

Java int cannot be converted to boolean

From Dev

int[][] cannot be converted into int

From Dev

Java JComboBox Incompatible Types: Cannot be converted to string

From Dev

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

From Dev

int cannot be converted to int []

From Dev

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

From Dev

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

Related Related

  1. 1

    incompatible types: int cannot be converted to T

  2. 2

    Java : incompatible types; int cannot be converted to string

  3. 3

    incompatible types: Node cannot be converted to Tab

  4. 4

    java.lang.Object cannot be converted to int

  5. 5

    Error: incompatible types: double cannot be converted to JTextField

  6. 6

    Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

  7. 7

    incompatible types: HomeFragment cannot be converted to Fragment in Android

  8. 8

    Error: incompatible types: Object cannot be converted to char

  9. 9

    incompatible types: void cannot be converted to int

  10. 10

    JAVA incompatible types: Object cannot be converted to my type

  11. 11

    incompatible types: BigInteger cannot be converted to int

  12. 12

    Incompatible types: Object cannot be converted to ParseObject

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

    error: incompatible types: MainFragment cannot be converted to Activity

  17. 17

    incompatible types: char[] cannot be converted to CharSequence

  18. 18

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

  19. 19

    incompatible types: FragmentDark cannot be converted to Fragment in Android

  20. 20

    Incompatible operand types between K and int Java

  21. 21

    incompatible types: BigInteger cannot be converted to int

  22. 22

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

  23. 23

    Java int cannot be converted to boolean

  24. 24

    int[][] cannot be converted into int

  25. 25

    Java JComboBox Incompatible Types: Cannot be converted to string

  26. 26

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

  27. 27

    int cannot be converted to int []

  28. 28

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

  29. 29

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

HotTag

Archive