+ 4

Binary Converter Java

So i have a problem with finishing this project this is how I did my code: import java.util.Scanner; //your code goes here public class Converter { public static 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=scr.nextIn(); System.out.println (Converter.toBinary(x)); } } But I don't get any output with this code! Would appreciate any advice, thank you in advance!

14th Jan 2021, 6:00 PM
Lara Mijanović
Lara Mijanović - avatar
11 ответов
0
You have some typos and return type is missing. Return type is String and the typos i spotted are on reading input so the line should be: int x = sc.nextInt();
14th Jan 2021, 6:18 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 11
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)); } }
1st Feb 2021, 6:53 AM
Alex Kremen
Alex Kremen - avatar
0
import java.util.Scanner; //your code goes here class Converter{ static String toBinary(int x){ String bin=""; while(x>0){ bin=(x%2)+bin; x/=2; } return bin; } } 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)); } }
12th Feb 2022, 4:59 PM
Piyush Kumar Sonboir
Piyush Kumar Sonboir - avatar
0
Hi everybody. Can anyone explain, why the type of returned value in the toBinary method is String? Shouldn't it return integer? Since we are working with integers? Or am I missing something?
29th Jun 2022, 1:03 PM
Weronika Rostworowska
Weronika Rostworowska - avatar
0
Ok. I've found kind of answer here: https://www.sololearn.com/Discuss/2923028/?ref=app But I still don't really get, why we use String to avoid exceptions with long numbers. String is, well... String. Doesn't have anything to do with numbers. ? Edit. Ok. After a long thinking, I think I understand. It's more memory efficient than using a return type long. I suppose.
29th Jun 2022, 1:14 PM
Weronika Rostworowska
0
We use string to make the binary digits visible. In memory everything is binary, even the characters in the string are encoded in 8 bits typically. But we want to see the ones and zeros so we put them in a string
29th Jun 2022, 3:59 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Enter a #: 5 a b c d e
28th Nov 2022, 5:33 AM
Aries Dumayag
Aries Dumayag - avatar
0
import java.util.Scanner; //your code goes here 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)); } }
17th Jan 2023, 6:11 AM
Harshika Madhushani
Harshika Madhushani - avatar
- 1
Thank you! Now it works!
14th Jan 2021, 6:25 PM
Lara Mijanović
Lara Mijanović - avatar
- 2
Tq
11th Jul 2021, 8:04 AM
Syed D
Syed D - avatar