+ 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?
23 ответов
+ 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.
+ 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.
+ 3
Wow Sakshi, you're my savior. Thank you so much!
+ 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.
+ 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;
}
}
+ 1
Ok, I return binary instead, but it doesn't work at all. :(((
+ 1
Maybe my code doesn't finish, and it needs more code
+ 1
I'm sorry but I can't fix it. It doesn't work
+ 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));
}
}
+ 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?
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;
}
}
0
I think I should sleep instead
0
Welcome Nguyen Gia
0
Sakshi , I run it , but it said invalid method ToT
0
I don't know why your code not run because I input the number 6 and it gives me output 110
0
You run your code on the IDE?
0
How about 42 its binary is 101010
0
No, I run in this app
0
:(( , you can run it on Converter Binary challenge it doesn't work at all
0
Run on Sololearn playground it's completely run