+ 1

Why does my BinaryConverter not work in all test cases correctly? (Java)

I have coded a Binary Converter, because it is a task of the Java tutorial here, and it went throught almost all test cases except for the 4th one, but Idk why. Can sb help me out?

6th Feb 2022, 5:58 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar
7 ответов
+ 2
Hello Sndr Brst🇩🇪 Your code is not working for higher numbers. So I would use long instead of int. Btw: .toString() can also be used as a converter Integer.toString(yourNum, base) (works also for Long and BigInteger) e.g. Integer.toString(7,2) returns "111"
6th Feb 2022, 6:18 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
import java.util.Scanner; public class Converter{ static long toBinary(long x){ long z=1; long y=0; while(x>0){ y+=(x%2) * z; z*= 10; x = x/2; } return y; } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); long x = sc.nextLong(); System.out.print(Converter.toBinary(x)); } }
6th Feb 2022, 6:33 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
for each primitives exists a wrapper class: int -> Integer long -> Long char -> Character and so on https://www.baeldung.com/java-wrapper-classes
6th Feb 2022, 6:41 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
https://code.sololearn.com/cD9OkAWe67zB/?ref=app
6th Feb 2022, 5:59 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar
+ 1
Denise Roßberg thank you very much😃
6th Feb 2022, 6:24 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar
0
Denise Roßberg and what do I type in stead of "integer"?
6th Feb 2022, 6:31 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar
0
thanks, this way it works, but I still don't get how an integer can be an object so that one can access it with dot notation in order to convert it to a String...
6th Feb 2022, 6:36 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar