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
15 Answers
New AnswerJavi 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.
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
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; } }
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.
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)); } }
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 💯
Where do you put the input number, without an ide I dont see it ? Sorry for such a noob question.
static StringtoBinary(int num){ String binary=""; while(num>0){ binary=(num%2)+binary; num/=2; } return binary
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.
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; } }
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