Jump to content

How do I fix this error: int cannot be converted to int[] (JAVA)

CreepyPro

Write a method named collapse that accepts an array of integers as a parameter and returns a new array where each pair of integers from the original array has been replaced by the sum of that pair. 

 

I have this so far, can someone explain me how to fix the error?

public static int collapse(int a[]){
    int a2[] = new int[a.length/2];
    if (a.length%2==0)    
        for (int i=0; i<a2.length;i=i+2)
            a2[i] = a[i] + a[i+1];
    else {
        for (int i=0; i<a2.length-1;i=i+2)
            a2[i] = a[i] + a[i+1];
        a2[a.length/2] = a[a.length];
    }
   return a2;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

You are trying to return an int array, which is also the objective you outlined in your post, but in the header of the method you write 'int', instead of 'int[]'. The latter of which would allow you to return an int array with this method.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Please try to actually read the error. It should include a line number (probably of the return statement). You declared the return type as int but are trying to return a int[].

 

My boring Github   /人◕ ‿‿ ◕人\

Link to comment
Share on other sites

Link to post
Share on other sites

Your return type is int instead of int[]... so change the return type...

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

public static int[] collapse(int a[])

 

I don't do Java but C# is close enough - so I think that's what the other people were suggesting you change :) (the thing in bold)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×