+ 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?
7 Respostas
+ 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"
+ 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));
}
}
+ 2
for each primitives exists a wrapper class:
int -> Integer
long -> Long
char -> Character
and so on
https://www.baeldung.com/java-wrapper-classes
+ 1
https://code.sololearn.com/cD9OkAWe67zB/?ref=app
+ 1
Denise Roßberg thank you very much😃
0
Denise Roßberg and what do I type in stead of "integer"?
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...