Wednesday, 17 April 2019

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

Problem:-

-2
This is my code for a problem that I am trying to solve
public static int totalchocolates(Integer[] input1) {

    int countEaten = 0;
    Arrays.sort(input1, Collections.reverseOrder());

    for (int i = input1.length - 1; i > -1; i--) {
        countEaten = (int)(countEaten + Math.ceil(input1[i].doubleValue() / 2));
        if (i > 1 && (input1[i - 1] + input1[i] / 2) > 1000) {
            i = i - 1;
        }
    }
    return countEaten;
}
The main fuction is
public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in);
    int output = 0;
    int ip1_size = 0;
    ip1_size = Integer.parseInt(in.nextLine().trim());
    int[] ip1 = new int[ip1_size];
    int ip1_item;
    for (int ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
        ip1_item = Integer.parseInt( in .nextLine().trim());
        ip1[ip1_i] = ip1_item;
    }
    output = totalchocolates(ip1);
    System.out.println(String.valueOf(output));
}
I am getting the following error,
CandidateCode.java:36: error: incompatible types: int[] cannot be converted to >Integer[] output = totalchocolates(ip1);

Solution:-

Error clearly says that you are providing int[] as input parameter where as your function is expecting Integer[] array. It would be better you change the input array to type Integer[]
Integer[] ip1 = new Integer[ip1_size];

No comments:

Post a Comment

Error While embed the video in Your website page

Error:- Refused to display '<URL>' in a frame because it set 'X-Frame-Options' to 'sameorigin Solution:- if ...