+ 2
Binary Converter Java
I was doing one of the projects in the java modules and this one of the binary converter doesn't pass all the tests, there's only one test that it doesn't pass and it's one of the hidden ones so I don't know in what is failling. I hope someone reads this and help me find the error, because I have no idea :/ https://code.sololearn.com/c12loxa4AU6r/?ref=app
16 Answers
0
Javi Tau
Return as a String not int.
Explanation:- As you are getting binary number as String so change return type int to String or convert String to int.
+ 53
import java.util.Scanner;
//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));
}
}
public class Converter {
public static String toBinary(int num) {
String binary = "";
while(num > 0) {
binary = (num % 2) + binary;
num /= 2;
}
return binary;
}
}
Here ya go m8
+ 8
There is another way that code can be done, is like this and it keeps it looking the way they explained it in the in the challange
public class Converter {
public static String toBinary(int num) {
String binary="";
while(num > 0) {
binary = (num%2)+binary;
num /= 2;
}
return binary;
}
}
+ 1
If you pass input as 0 you will get runtime error. That is because you are parsing integer from empty string. And as I Am Groot! said return string since binary of even small numbers length will be very large.
+ 1
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;
}
}
//ur 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));
}
}
0
import java.util.Scanner;
//your code goes here
class Converter {
static int[] addToArray(int arr[], int x) {
int[] newArray = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArray[i] = arr[i];
}
newArray[arr.length] = x;
return newArray;
}
static int ArrayToInteger(int arr[]) {
int Integer = 0;
for (int i = arr.length; i > 0; i--) {
int powerOfTen = (int)Math.pow(10, i - 1);
Integer += arr[arr.length - i] * powerOfTen;
}
return Integer;
}
static int flipAndConvertToInteger(int arr[]) {
int[] newArray = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
newArray[arr.length - (i + 1)] = arr[i];
}
int output = ArrayToInteger(newArray);
return output;
}
public static int toBinary(int x) {
int[] binaryArray = new int[0];
int binaryRemainder = 0;
do {
binaryRemainder = x % 2;
binaryArray = addToArray(binaryArray, binaryRemainder);
x /= 2;
} while (x != 0);
return (flipAndConvertToInteger(binaryArray));
}
}
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));
sc.close();
}
}
My code's a little long, unfortunately, because I didn't see the string part and went the array way but it still doesn't clear the 4th test. Please help.
0
This is my code:
import java.util.Scanner;
//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));
}
}
public class Converter {
public static String toBinary(int num) {
String binary = "";
while(num > 0) {
binary = (num % 2) + binary;
num /= 2;
}
return binary;
}
}
This is correct 💯
0
Can someone explain me why the method toBinary() have to be String, the output is a integer or not? Sorry am a bit confused right now
0
who use int beside String, it's me wkwkkwk
- 1
does anyone know what the test #4 is? Cant pass this one only.
- 1
Where do you put the input number, without an ide I dont see it ?
Sorry for such a noob question.
- 1
you dont, the test will do it for you
- 1
static StringtoBinary(int num){
String binary="";
while(num>0){
binary=(num%2)+binary;
num/=2;
}
return binary
- 1
can someone explain line of code <String binary = "";>
- 1
why the following code is not working :
import java.util.Scanner;
//Binary Converter solution in Java from sololearn
//your code goes here
import java.util.Arrays;
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));
}
}
public class Converter {
public int toBinary(int num) {
int binary ;
while(num > 0) {
binary = (num % 2) + binary;
num /= 2;
}
return binary;
}
}
- 3
The binary converter
import java.util.Scanner;
//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));
}
}
public class Converter {
public static String toBinary(int num) {
String binary = "";
while(num > 0) {
binary = (num % 2) + binary;
num /= 2;
}
return binary;
}
}
Thid od the right answer