【发布时间】:2014-10-30 19:28:51
【问题描述】:
我正在编写一个在十进制、二进制和十六进制之间转换的程序。程序可以编译,但是当我输入二进制到十六进制时,我得到一个异常“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:34”。这是第一个问题。第二个问题是十六进制到二进制的转换给了我一些可笑的冗长(而且不正确)的返回。我已经包含了两者的代码。非常感谢您朝正确的方向轻推。
二进制转十六进制:
/**
* Method that converts a binary number to its hexadecimal equivalent.
* @param no parameters
* @return returns void
*/
public void binToHex()
{
System.out.println("The binary number you enter will be converted to its hexidecimal equivalent.");
System.out.println("Please enter a binary number: ");
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.replace(" ", "").trim();
StringBuffer hex = new StringBuffer("00000000000000000000000000000000");
//String hex1 = "";
int j = 0;
for (int i = 0; i < bin.length(); i++)
{
if (bin.substring(i, i+4).equals("0000"))
{
hex.setCharAt(j, '0');
}
else if (bin.substring(i, i+4).equals("0001"))
{
hex.setCharAt(j, '1');
}
else if (bin.substring(i, i+4).equals("0010"))
{
hex.setCharAt(j, '2');
}
else if (bin.substring(i, i+4).equals("0011"))
{
hex.setCharAt(j, '3');
}
else if (bin.substring(i, i+4).equals("0100"))
{
hex.setCharAt(j, '4');
}
else if (bin.substring(i, i+4).equals("0101"))
{
hex.setCharAt(j, '5');
}
else if (bin.substring(i, i+4).equals("0110"))
{
hex.setCharAt(j, '6');
}
else if (bin.substring(i, i+4).equals("0111"))
{
hex.setCharAt(j, '7');
}
else if (bin.substring(i, i+4).equals("1000"))
{
hex.setCharAt(j, '8');
}
else if (bin.substring(i, i+4).equals("1001"))
{
hex.setCharAt(j, '9');
}
else if (bin.substring(i, i+4).equals("1010"))
{
hex.setCharAt(j, 'A');
}
else if (bin.substring(i, i+4).equals("1011"))
{
hex.setCharAt(j, 'B');
}
else if (bin.substring(i, i+4).equals("1100"))
{
hex.setCharAt(j, 'C');
}
else if (bin.substring(i, i+4).equals("1101"))
{
hex.setCharAt(j, 'D');
}
else if (bin.substring(i, i+4).equals("1110"))
{
hex.setCharAt(j, 'E');
}
else if(bin.substring(i, i+4).equals("1111"))
{
hex.setCharAt(j, 'F');
}
i = i + 4;
j = j + 1;
}
System.out.println("The binary number you entered, " + oldbin + " is " + hex + " in hexadecimal.\n");
pw.print("The binary number you entered, " + oldbin + " is " + hex + " in hexadecimal.\n");
}
}
十六进制转二进制:
/**
* Method that converts a hexadecimal number to its binary equivalent.
* @param no parameters
* @return returns void
*/
public void hexToBin()
{
System.out.println("The hexadecimal number you enter will be convered to its binary equivalent.");
System.out.println("Please enter a hexadecimal number: ");
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
bin = bin.trim();
String binary = "";
for (int i = 0; i < bin.length(); i++)
{
if(bin.charAt(i) == '0')
{
binary = binary.concat("0000");
}
else if(bin.charAt(i) == '1')
{
binary = binary.concat("0001");
}
else if(bin.charAt(i) == '2')
{
binary = binary.concat("0010");
}
else if(bin.charAt(i) == '3')
{
binary = binary.concat("0011");
}
else if(bin.charAt(i) == '4')
{
binary = binary.concat("0100");
}
else if(bin.charAt(i) == '5')
{
binary = binary.concat("0101");
}
else if(bin.charAt(i) == '6')
{
binary = binary.concat("0110");
}
else if(bin.charAt(i) == '7')
{
binary = binary.concat("0111");
}
else if(bin.charAt(i) == '8')
{
binary = binary.concat("1000");
}
else if(bin.charAt(i) == '9')
{
binary = binary.concat("1001");
}
else if(bin.charAt(i) == 'A');
{
binary = binary.concat("1010");
}
if(bin.charAt(i) == 'B');
{
binary = binary.concat("1011");
}
if(bin.charAt(i) == 'C');
{
binary = binary.concat("1100");
}
if(bin.charAt(i) == 'D');
{
binary = binary.concat("1101");
}
if(bin.charAt(i) == 'E');
{
binary = binary.concat("1110");
}
if(bin.charAt(i) == 'F');
{
binary = binary.concat("1111");
}
}
System.out.println("The hexadecimal you entered, " + bin + " is " + binary + " in binary.\n");
pw.print("The hexadecimal you entered, " + bin + " is " + binary + " in binary.\n");
}
}
【问题讨论】:
-
为什么不使用
toBinaryString()、toHexString()和带基数的parseInt()? -
从 'A' 大小写开始,紧跟在
else if条件后面的多余分号应该被删除。 -
这是一个作业,我不允许使用任何 Java 的内置库函数进行转换。
-
使用调试器单步调试代码或插入 println 语句以找出发生了 StringIndexOutOfBounds 错误的原因。这是一个简单的调试任务,你需要学习去做。请注意,异常堆栈跟踪会准确告诉您出现此错误的位置,因此这不是寻找复活节彩蛋。
-
我能弄明白。现在我有一个不同的问题,我似乎无法解决。感谢您的所有帮助!