This is my code, it is so much easier to read then the solution provided and when I add random hex values in it does correctly convert to decimal.
import java.util.Arrays;
public class Solution {
public static String[] hexces = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
public static String[] bintable = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
public static void main(String[] args) {
String binaryNumber = "100111010000";
System.out.println("Binary number " + binaryNumber + " is equal to hexadecimal number " + toHex(binaryNumber));
String hexNumber = "8ef690";
System.out.println("Hexadecimal number " + hexNumber + " is equal to binary number " + toBinary(hexNumber));
}
public static String toHex(String binaryNumber) {
//write your code here
boolean only1and0 = true;
String binaryReturn = "";
/*for (int i = 0; i < binaryNumber.length(); i++)
{
if(binaryNumber.charAt(i) != '0' || binaryNumber.charAt(i) != '1')
{
only1and0 = false;
}
}*/
if (binaryNumber == null) {
return binaryReturn;
}
if (binaryNumber.length() % 4 != 0)
{
for (int i = 0; i < binaryNumber.length() % 4; i++)
{
binaryNumber = '0' + binaryNumber.substring(0,binaryNumber.length());
}
System.out.println(binaryNumber);
}
for (int i = 0; i < binaryNumber.length(); i = i+ 4)
{
String nibble = binaryNumber.substring(i,i+4);
int binIndex = Arrays.asList(bintable).indexOf(nibble);
binaryReturn = binaryReturn + hexces[binIndex];
}
return binaryReturn;
}
public static String toBinary(String hexNumber) {
//write your code here
String hexReturn = "";
if (hexNumber == null)
{
return hexReturn;
}
for (int i = 0; i < hexNumber.length(); i++)
{
int hexIndex = Arrays.asList(hexces).indexOf(hexNumber.charAt(i) + "");
hexReturn = hexReturn + bintable[hexIndex];
}
return hexReturn;
}
}
Why is this code not acceptable
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Richard
18 October, 19:37
@Thomas
How do you do to read in into someones code?
Do you have a approach or hint for me?
0
Thomas
19 October, 07:11
It's probably like many things, you have to do it, practice it. For a start you can pick out code that you have already programmed yourself. Then you already know a solution and other approaches are easier to understand. And it's sometimes really surprising what ideas other programmers come up with (positive and negative).
You may not be able to understand some code because you may not even know the syntax yet. But here you can try to get a rough overview of the concept.
It also helps of course to run the code, to set debug points (or use logging) and output variables and observe the code flow.
When it gets more difficult, you write tests. At the beginning simply code in the main method. Later you use test frameworks.
As I said, you have to learn it and it is sometimes helpful.
A friend of mine is so good at reading other people's code that he is now only used for this purpose in his company. He reads code and tries to find bugs that others can't fix.
0
Thomas
19 October, 07:21
Tests:
Sometimes Codegym already offers a super simple test that only covers the standard, but no edge cases. Or you have to enter a filename using the keyboard. In such cases, you take out this keyboard input for your own tests and add edge cases, an example (just quickly written down, so no reference):
With one run you have tested everything, each digit, connected digits (length still may be a problem) and also input that should result in an empty string. It also comes with some validation. 0
Thomas
13 October, 07:57
Reason for not validation may be the fact that you didn't implement all requirements (checking for valid input). If I pass eg "3" to the toHex() method, then it throws an exception instead of returning "". If I enable the commented out check, then this will not do anything but the code throws a NullPointerException if I pass null to that method.
The toBinary() method also has no check for valid input. Eg. "world" will throw an OutOfBoundsException
0