如果构造函数中的if语句无法正常运行

时间:2018-11-02 16:43:33

标签: java string if-statement constructor hex

在我的程序中,我必须将十进制整数,二进制字符串或十六进制字符串放入类中。然后,它将变量转换为其他变量。

我的问题是,由于二进制和十六进制都是字符串,因此我让构造函数确定参数是二进制还是十六进制,然后从那里继续。但是,根据我的错误,程序无法确定十六进制数是什么。

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

这是错误:

  

线程“主”中的异常java.lang.NumberFormatException:用于输入   字符串:“ x”    在   java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

     

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

     

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

     

在BDHNumber.convertBToD(BDHNumber.java:63)

错误发生在convertBToD方法中,特别是在将二进制数的一位转换为整数的情况下。由于某种原因,我的代码在构造函数中将十六进制数作为二进制数,而不是遵循if语句。它无法将“ x”转换为整数,因此有错误。我只是不知道为什么。任何多余的眼睛都会有很大的帮助。

0 个答案:

没有答案