Clyde Gustafson
Clyde Gustafson

Reputation: 63

Error: incompatible types: Integer cannot be converted to Integer[]

I'm attempting to create a method that will return an index within an array.

private static Integer[] day1 = new Integer[6];

public Integer[] getDay1(Integer team) {
      return day1[team];
}

When I try to do this, however, it highlights the opening bracket following day1 with this error.

Days.java:32: error: incompatible types: Integer cannot be converted to Integer[]
  return day1[team];
             ^

Any idea why this is? Any help would be appreciated

Upvotes: 0

Views: 4455

Answers (3)

Marvin
Marvin

Reputation: 14415

Integer is one integer value, Integer[] is an array of single Integers. The signature of your getDay1 method promises an array as return value, but you are actually returning one single element (the one at position team).

I assume you are looking for

public Integer getDay1(Integer team) {
      return day1[team];
}

Upvotes: 1

svarog
svarog

Reputation: 9847

you are returning an Integer when you have defined your method to return an array or integers, either change to

public Integer getDay1(Integer team) {
  return day1[team];
}

or

public Integer[] getDay1(Integer team) {
  return day1;
}

Upvotes: 1

Kristof
Kristof

Reputation: 249

return day1[team]; will return the teamth value of your array. However the return type of your method is Integer[] (array). Change your method to:

public Integer getDay1(Integer team) {
      return day1[team];
}

Upvotes: 2

Related Questions