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

Spi*_*lia 5 java string hex constructor if-statement

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

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

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());
   }
}
Run Code Online (Sandbox Code Playgroud)
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;
}
} 
Run Code Online (Sandbox Code Playgroud)

这是错误:

线程“main”中的异常 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”转换为整数,因此出现错误。我就是不明白为什么。任何额外的眼睛都会有很大的帮助。

Rei*_*las 0

在 Java 中,要将十六进制值转换为整数,可以使用该Integer.parseInt方法,第二个参数为16基数。

0x它不会识别以十六进制开头的值。

此外,您正在convertBToD以下条件块下进行调用。
我相信你的意思是convertHToD

if (stringNumber.startsWith("0x")){
    theNumberAsHexadecimal = stringNumber;
    theNumberAsBinary = convertHToB(theNumberAsHexadecimal);
    theNumberAsDecimal = convertBToD(theNumberAsBinary);
}
Run Code Online (Sandbox Code Playgroud)

例子

int convertHToD(String hexadecimal) {
    return Integer.parseInt(hexadecimal.substring(2), 16);
}
Run Code Online (Sandbox Code Playgroud)