公共类Question2 {
//To convert INSERT NUMBER HERE to hexadecimal
public static String binarytoHex(String number, int bin, int hex) {
// Parse the number with source radix and return in specified radix(base)
return Integer.toString(Integer.parseInt(number, bin), hex);
}
public static void main(String[] args) {
String x = "before"; // digits before decimal
String y = "after"; //digits after the decimal
int bin = 2; // Source Base
int hex = 16; // Destination Base Hexadecimal
System.out.println("Binary to Hexadecimal: "+ binarytoHex(x, bin, hex) + "." + binarytoHex(y, bin, hex));
}
}
这是控制台中显示的错误:线程“主”中的异常java.lang.NumberFormatException:对于输入字符串:基数2下的“ before”在java.base / java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)在java.base / java.lang.Integer.parseInt(Integer.java:658)在mathsassignment / mathsassignment.Question2.binarytoHex(Question2.java:9)在mathsassignment / mathsassignment.Question2.main(Question2.java:20)
您正在将不是数字的字符串传递给binaryToHex方法。阅读错误消息。这很有帮助。字符串x需要分配一个数字。 String x = "12";