Binary Converter Java | Sololearn: Learn to code for FREE!

+1

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

12/27/2020 4:18:55 AM

Javi Tau

13 Answers

New Answer

+3

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.

+27

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

+11

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; } }

+3

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)); } }

+2

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.

0

Try... //tu código va aquí public class Converter { public static String toBinary(int num){ return Integer.toBinaryString(num); } }

0

does anyone know what the test #4 is? Cant pass this one only.

0

Where do you put the input number, without an ide I dont see it ? Sorry for such a noob question.

0

you dont, the test will do it for you

0

static StringtoBinary(int num){ String binary=""; while(num>0){ binary=(num%2)+binary; num/=2; } return binary

0

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

0

can someone explain line of code <String binary = "";>

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.