+ 2

Problem with my code

Hey guys! I tried to do the Binary Converter in Java Chapter 4 but it doesnt work! Some1 knows what I did wrong? This is my code : 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)); } } Hope someone could help!

22nd Feb 2021, 4:22 PM
Kevin S
Kevin S - avatar
8 odpowiedzi
22nd Feb 2021, 4:40 PM
JaScript
JaScript - avatar
+ 1
This is also okay.
22nd Feb 2021, 5:40 PM
JaScript
JaScript - avatar
+ 1
You should write the class without (): public class Converter { https://code.sololearn.com/cbq12cROX96D/?ref=app
22nd Feb 2021, 5:47 PM
JaScript
JaScript - avatar
+ 1
Oh wow...yes , I didnt see that. Thank you!
22nd Feb 2021, 5:54 PM
Kevin S
Kevin S - avatar
+ 1
By the first reading I didnt see it too.
22nd Feb 2021, 6:10 PM
JaScript
JaScript - avatar
+ 1
You can try this maybe it helps. 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; } } //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)); } }
22nd Feb 2021, 10:34 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
0
In the task it says that you can program it with this : String binary=""; while(num > 0) { binary = (num%2)+binary; num /= 2; } I guess this is wrong or? Or can you do it with that?
22nd Feb 2021, 4:58 PM
Kevin S
Kevin S - avatar
0
But I used it in my code and it wasnt right :/ I dont even get an output , you know why?
22nd Feb 2021, 5:45 PM
Kevin S
Kevin S - avatar