vendredi 2 novembre 2018

If statement in my constructor is not functioning properly

In my program, I have to put a decimal integer, a binary string, or a hexadecimal string into my class. It then converts the variable into the others.

My issue is that, since binary and hexadecimal are both strings, I have the constructor determine whether the argument is a binary or hexadecimal and proceed from there. However, based on my error, the program isn't figuring out a hexadecimal number is what it is.

import java.util.*;
class Tester{
   public static void main(String[] args){
      String hex1 = "0x34";
      BDHNumber test = new BDHNumber(hex1);

      System.out.println(test.getDec());
      System.out.println(test.getBin());
      System.out.println(test.getHex());
   }
}


import java.util.*;
import java.lang.*;

public class BDHNumber{
   private int theNumberAsDecimal;
   private String theNumberAsBinary;
   private String theNumberAsHexadecimal;
   private static String[] hexadecimalArray = {"0", "1", "2", "3", "4", "5", 
                                               "6", "7", "8", "9", "A", "B", 
                                               "C", "D", "E", "F"};
   private static String[] binaryArray = {"0000", "0001", "0010", "0011", 
                                          "0100", "0101", "0110", "0111", 
                                          "1000", "1001", "1010", "1011",
                                          "1100", "1101", "1110", "1111"}; 

//Default empty constructor.
public BDHNumber(){
}

//Decimal constructor.
public BDHNumber(int decimal){
   theNumberAsDecimal = decimal;
   theNumberAsBinary = convertDToB(decimal);
   theNumberAsHexadecimal = convertBToH(theNumberAsBinary);
}

//Constructs binary or hexadecimal.
public BDHNumber(String stringNumber){ 
   if (stringNumber.startsWith("0x")){
       theNumberAsHexadecimal = stringNumber;
       theNumberAsBinary = convertHToB(theNumberAsHexadecimal);
       theNumberAsDecimal = convertBToD(theNumberAsBinary);
   }
   else{
       theNumberAsBinary = stringNumber;

       //Pads the binary to be multiples of 4
       while(theNumberAsBinary.length()%4 != 0){
        theNumberAsBinary = "0" + theNumberAsBinary;
       }

      theNumberAsDecimal = convertBToD(theNumberAsBinary);
      theNumberAsHexadecimal = convertBToH(theNumberAsBinary);
   }
}

//Converts Binary to Decimal.
private static int convertBToD(String binary){
   String binary1 = binary;
   int decimal = 0;
   int index = 0;

   //Pads the binary to be multiples of 4
   while(binary1.length()%4 != 0){
         binary1 = "0" + binary1;
      }

   int length = binary1.length();

   for(int i = length; i > 0; i--){
      decimal = decimal + (int)(Integer.parseInt(binary1.substring(index,
                                index+1)) * Math.pow(2, (i-1)));
      index++;
   }
   return decimal;
}

//Converts Binary to Hexadecimal.
private static String convertBToH(String binary){
   String binary1 = binary;
   String hex = "";
   int index = 0;
   int binaryAsDecimal = 0; 

   //Pads the binary to be multiples of 4
   while(binary1.length()%4 != 0){ 
         binary1 = "0" + binary1;
      }

   int length = binary1.length();

   while(length != 0){
      binaryAsDecimal = convertBToD(binary1.substring(length-4, length));

      for(int i = 0; i<hexadecimalArray.length; i++){
         if(i == binaryAsDecimal){
            hex = hexadecimalArray[i] + hex;
         }
      }
      length = length - 4;
   }
   return hex;
}

//Converts Decimal to Binary
private static String convertDToB(int decimal){
   int decimal1 = decimal;
   int remainder;
   String binary = "";

   while (decimal != 0){
      remainder = decimal % 2; 

      if (remainder == 1){
         binary = "1" + binary;
      }
      else if (remainder == 0){
         binary = "0" + binary;
      }
      decimal = decimal / 2;
   }
   return binary;
}

//Converts Hexadecimal to Binary.
private static String convertHToB(String hexadecimal){
   String hex = hexadecimal;
   String binary = "";
   int index = 0;
   int length = hex.length();

   for(int i = hex.length(); i > 0; i--){
      if(hexadecimalArray[index] == hex.substring(length-1, length)){
         binary = convertDToB(index) + binary;
      }
      index++;
      length--;
   }
   return hex;
}

//Getters.
public String getBin(){
   return theNumberAsBinary;
}

public String getHex(){
   return theNumberAsHexadecimal;
}

public int getDec(){
   return theNumberAsDecimal;
}
} 

Here is the error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "x" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580)

at java.lang.Integer.parseInt(Integer.java:615)

at BDHNumber.convertBToD(BDHNumber.java:63)

The error is in the convertBToD method, and specifically where it converts one bit of the binary number to an integer. For some reason my code is taking the hexadecimal number as a binary in the constructor, instead of following the if statement. It can't convert "x" into an integer, so it has an error. I just can't figure out why. Any extra eyes would be a great help.

Aucun commentaire:

Enregistrer un commentaire