+ 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

27th Dec 2020, 4:18 AM
Javi Tau
Javi Tau - avatar
16 Respuestas
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.
27th Dec 2020, 4:32 AM
A͢J
A͢J - avatar
+ 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
19th Feb 2021, 5:38 AM
Deniz
Deniz - avatar
+ 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; } }
5th Jan 2021, 2:00 AM
Erik Jensen
Erik Jensen - avatar
+ 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.
27th Dec 2020, 4:37 AM
Padala Vamsi
Padala Vamsi - avatar
+ 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)); } }
17th Apr 2021, 1:46 PM
Hemasai Rajulapati
Hemasai Rajulapati - avatar
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.
24th Sep 2021, 2:44 PM
Mayfly
Mayfly - avatar
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 💯
12th Dec 2021, 1:36 AM
MN FATHIMA RIHAM - s92064451 -321424451
MN FATHIMA RIHAM - s92064451 -321424451 - avatar
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
8th Sep 2022, 10:02 AM
Viana Isabella
Viana Isabella - avatar
0
who use int beside String, it's me wkwkkwk
18th Nov 2022, 10:04 AM
Mukhamad Firdaus
Mukhamad Firdaus - avatar
- 1
does anyone know what the test #4 is? Cant pass this one only.
14th Jan 2021, 7:28 AM
Bartek Rocławski
Bartek Rocławski - avatar
- 1
Where do you put the input number, without an ide I dont see it ? Sorry for such a noob question.
20th Jan 2021, 9:17 AM
Chris N
- 1
you dont, the test will do it for you
20th Jan 2021, 9:18 AM
Bartek Rocławski
Bartek Rocławski - avatar
- 1
static StringtoBinary(int num){ String binary=""; while(num>0){ binary=(num%2)+binary; num/=2; } return binary
2nd Feb 2021, 10:37 AM
Milni Nanayakkara
Milni Nanayakkara - avatar
- 1
can someone explain line of code <String binary = "";>
8th Jul 2021, 6:27 AM
Poojary Karunakar Ramanatha
Poojary Karunakar Ramanatha - avatar
- 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; } }
28th Mar 2022, 6:33 PM
Osama Ghandour Geris
Osama Ghandour Geris - avatar
- 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
26th Mar 2021, 10:15 AM
ANIKET SINGH
ANIKET SINGH - avatar