0

Can I get some help? I have been trying to solve the Java course Binary Converter Code Project for 2 weeks now.

https://code.sololearn.com/c5wBz5Up7Ve5/?ref=app I want to return the binary of x, but it doesn't run at all. It says "invalid method declaration;return type required"

25th Oct 2021, 11:25 PM
David Osam
David Osam - avatar
11 Respuestas
+ 3
This error occurs because your toBinary method returns an integer but you didn’t put the declaration before the function name. It should be: Public static int toBinary However, you shouldn’t return your num variable, but binary variable. So the method should be: public static String toBinary return binary;
25th Oct 2021, 11:33 PM
Guillem Padilla
Guillem Padilla - avatar
+ 2
Wheew alright thanks a lot!
25th Oct 2021, 11:51 PM
David Osam
David Osam - avatar
+ 1
O ok thanks, however...Even though it runs now, it only always returns zero
25th Oct 2021, 11:38 PM
David Osam
David Osam - avatar
+ 1
I edited my answer, read it again please :p
25th Oct 2021, 11:38 PM
Guillem Padilla
Guillem Padilla - avatar
+ 1
The Code Project provided a code for that, but why's it always returning zero instead of the binary version
25th Oct 2021, 11:39 PM
David Osam
David Osam - avatar
+ 1
As I said you’re returning num variable, and you should return binary variable
25th Oct 2021, 11:40 PM
Guillem Padilla
Guillem Padilla - avatar
+ 1
Ok sorry I just saw that you had edited it. Alright I have done that but, String binary has already been defined in the method, that's the error message I am receiving now when I declare the String binary in the bracket as in toBinary(String binary)
25th Oct 2021, 11:44 PM
David Osam
David Osam - avatar
+ 1
I have solved it thanks
25th Oct 2021, 11:46 PM
David Osam
David Osam - avatar
+ 1
I should have maintained the int num in as in toBinary(int num). So when I got that error message: String binary has already been defined - I realized that, that I was doing the wrong thing by trying to pass the String binary as an argument. The goal is to pass the integer num instead
25th Oct 2021, 11:50 PM
David Osam
David Osam - avatar
+ 1
;) congratz!!
25th Oct 2021, 11:57 PM
Guillem Padilla
Guillem Padilla - avatar
0
Here is a solution using recursion: https://code.sololearn.com/cxRQ7uWbNUom/?ref=app
14th Jan 2022, 6:45 PM
Paul K Sadler
Paul K Sadler - avatar