Can anyone give me the code of ConverterBinary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anyone give me the code of ConverterBinary?

I can't write the right code. It always said I dont declare the method toBinary(), but I declare it as public static toBinary().I dont know what to do. Can anyone help me?

20th Nov 2022, 4:19 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
23 Answers
+ 4
Your method toBinary is missing return type. And note that you are returning x which value is 0 after loop. Instead return 'binary' value.
20th Nov 2022, 4:42 PM
Jayakrishna 🇮🇳
+ 3
Your code almost finished the task. But mistake is "you are not mentioned return type for method. Add depends on the return type. Your result is in binary variable so return binary; if you add return type as int then convert binary value into inter type before returning. But note this does not pass all test cases because of integer overflow as you need there is Long type. So better simply make return type String and return binary; hope it helps... edit: if not work, then share your changed code.
20th Nov 2022, 5:08 PM
Jayakrishna 🇮🇳
+ 3
Wow Sakshi, you're my savior. Thank you so much!
20th Nov 2022, 5:18 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
+ 2
Nguyen Gia Pls edit your question description and add: 1. The task description. We need to know what you're trying to solve. 2. A link to your code in Code Playground. We need to see your code as it is, and run it.
21st Nov 2022, 12:34 AM
Emerson Prado
Emerson Prado - avatar
+ 1
This is my code: import java.util.Scanner; //your code goes here public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } } public class Converter { public static toBinary (int x){ String binary = ""; while(x>0){ binary = (x%2) + binary ; x/=2; } return x; } }
20th Nov 2022, 4:21 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
+ 1
Ok, I return binary instead, but it doesn't work at all. :(((
20th Nov 2022, 4:48 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
+ 1
Maybe my code doesn't finish, and it needs more code
20th Nov 2022, 5:01 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
+ 1
I'm sorry but I can't fix it. It doesn't work
20th Nov 2022, 5:16 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
+ 1
This is my code. You can try this out. import java.util.Scanner; public class Converter{ public static String toBinary(int num){ String binary=""; while(num > 0) { binary = (num%2)+binary; num /= 2; } return binary ; } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } }
21st Nov 2022, 4:03 AM
Devishree
Devishree - avatar
+ 1
Nguyen Gia Why don't you read an error message that clearly spells out the cause of the error and even the exact location of the error? — ... invalid method declaration; return type required: public static toBinary (int x){ ^ So you see the void type of the main() function because it doesn't return anything, but prints it out: public static void main(String[ ] args) { ... System.out.print(Converter.toBinary(x); } Prints the result of the toBinary() method, so the toBinary() method should return some value, in your case, the value of the "binary" variable of the "String" type means that the type of the toBinary() method must also be of type "String": public static String toBinary(int x){ String binary = ""; ... return binary; } But you return the "x" argument by some incomprehensible logic. 🤔 Why, then, did you convert it to the "binary" variable?
22nd Nov 2022, 2:32 AM
Solo
Solo - avatar
0
Now your code will run:- import java.util.Scanner; //your code goes here public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } } public class Converter { public static int toBinary (int x){ String binary = ""; while(x>0){ binary = (x%2) + binary ; x/=2; } int b = Integer.parseInt(binary); return b; } }
20th Nov 2022, 5:16 PM
Sakshi
Sakshi - avatar
0
I think I should sleep instead
20th Nov 2022, 5:16 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
0
Welcome Nguyen Gia
20th Nov 2022, 5:19 PM
Sakshi
Sakshi - avatar
0
Sakshi , I run it , but it said invalid method ToT
20th Nov 2022, 5:24 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
0
I don't know why your code not run because I input the number 6 and it gives me output 110
20th Nov 2022, 5:32 PM
Sakshi
Sakshi - avatar
0
You run your code on the IDE?
20th Nov 2022, 5:33 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
0
How about 42 its binary is 101010
20th Nov 2022, 5:34 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
0
No, I run in this app
20th Nov 2022, 5:34 PM
Sakshi
Sakshi - avatar
0
:(( , you can run it on Converter Binary challenge it doesn't work at all
20th Nov 2022, 5:36 PM
Nguyen Thi Thao
Nguyen Thi Thao - avatar
0
Run on Sololearn playground it's completely run
20th Nov 2022, 5:38 PM
Sakshi
Sakshi - avatar