There are several ways to do this in Java:
1) Use the framework:
Java lang.Integer.toBinaryString() method - GeeksforGeeks[
^]
2) Do it yourself.
The first is trivial, and probably not allowed for your homework!
The second means you need to read the question more carefully.
It specifically asks you to create a method that accepts an integer parameter and returns a string: your's doesn't do that. So start by getting the method signature right:
public static String ToBinary(int x)
{
return ...
}
Which means that instead of an array of integers, you need a string to "build into".
I'd start with two loops: one to find the most significant non-zero bit then one to convert the rest. That way, you don't have to "reverse" anything, which makes the task simpler.
Think about it, and then see how far you can get!