如果我的构造函数中的语句无法正常工作

If statement in my constructor is not functioning properly

提问人:Spirlia 提问时间:11/3/2018 更新时间:5/23/2023 访问量:125

问:

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

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

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

这是错误:

线程“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”转换为整数,因此存在错误。我只是想不通为什么。任何额外的眼睛都会有很大帮助。

Java String IF-statement 构造函数 进制

评论

1赞 001 11/3/2018
“x”表示您正在将十六进制字符串传递给 。遍历并检查所有调用,以查看您传递十六进制字符串的位置。提示:看。convertBToDconvertBToDconvertHToB
0赞 Spirlia 11/3/2018
我不知道我怎么直到现在才注意到退货声明是错误的,非常感谢。

答:

0赞 Reilas 5/23/2023 #1

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

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

此外,您正在以下条件块下调用。
我相信你的意思是.
convertBToDconvertHToD

if (stringNumber.startsWith("0x")){
    theNumberAsHexadecimal = stringNumber;
    theNumberAsBinary = convertHToB(theNumberAsHexadecimal);
    theNumberAsDecimal = convertBToD(theNumberAsBinary);
}

int convertHToD(String hexadecimal) {
    return Integer.parseInt(hexadecimal.substring(2), 16);
}