Why does my code not return? (Solved!) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

6th Dec 2022, 1:44 PM
Evan Xiang
4 Answers
+ 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
6th Dec 2022, 1:48 PM
Lisa
Lisa - avatar
+ 3
Yes, put your Converter class outside of the Program class
6th Dec 2022, 5:42 PM
Lisa
Lisa - avatar
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.
6th Dec 2022, 5:28 PM
Evan Xiang
0
Never mind! I realized I put the class Converter inside the public class Program. Thank you!
6th Dec 2022, 5:37 PM
Evan Xiang