0

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

3
  • Because you're not returning an array of Integers, you're returning a single Integer from the array. Commented Apr 26, 2015 at 20:00
  • Which do you want to return? An array or an integer? Commented Apr 26, 2015 at 20:00
  • Just the single int, and that worked. Commented Apr 26, 2015 at 20:05

3 Answers 3

2

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];
}
Sign up to request clarification or add additional context in comments.

3 Comments

beat me to it by 7 seconds :)
Mentally kicking myself for not seeing this.
the exception actually told you what's wrong in your code :)
1

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;
}

Comments

1

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];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.