0
Why does my code not return? (Solved!)
//my code import java.util.Scanner; 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)); } class Converter { public static toBinary() { String binary=""; while(num > 0) { binary = (num%2)+binary; num /= 2; } return binary; } } } it just gives me this error: /usercode/Program.java:12: error: invalid method declaration; return type required public static toBinary() { ^ 1 error
4 Respuestas
+ 6
public static ____ toBinary(____)
In the first blank space, you need to declare what type of value should be returned from the method – in this case, a string.
In the second blank space, you need create a slot for passing your number as an argument to the method. In this case, probably int num
+ 3
Yes, put your Converter class outside of the Program class
0
Thank you for your response, however:
I get this message after adding the String and int num:
/usercode/Program.java:12: error: Illegal static declaration in inner class Program.Converter
public static String toBinary(int num) {
^
modifier 'static' is only allowed in constant variable declarations
1 error
I tried adding "final" after "public static" but it continues to show that message.
0
Never mind! I realized I put the class Converter inside the public class Program. Thank you!