#Why does this code give me an error?

1 messages · Page 1 of 1 (latest)

tawny quartz
#

I am trying to train for java, but this code outputs an error

What is wrong?

public class main{
  public static int[] invert(int[] array) {
    for (int number : invert) {
      new_int.add(number*-1);
    }
    return new_int[]{};
  }
}
vague hollyBOT
#

Helper please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I am trying to train for java, but this code outputs an error What is wrong? ``` public class main{ public static int[] invert(int[] array) { for (int number : invert) { new_int.add(number*-1); } return new_int[]{}; } } ```

The issue in the code is that the for loop is incorrectly attempting to iterate over the array 'invert' instead of 'array'. To fix this, change 'for (int number : invert)' to 'for (int number : array)'.

Useful links:

shy spire
#

It's an array and not a list. Therefore it has no methods like 'add'

tawny quartz
#

ahhh

vague hollyBOT
#

Changed the category to Java.

#

<@&987246399047479336> please have a look, thanks.

shy spire
#

You have three different names here

unkempt belfry
#

also new_int and invert doesnt exist here

tawny quartz
#

i just copied the code

shy spire
#

It's mostly nonsense.

tawny quartz
#

well thats what i got

shy spire
#

Invert, for a number, means find it's reciprocal. And doing that on an int is going to end poorly.