我正在编写一个在十进制,二进制和十六进制之间转换的程序。程序编译,但是当我输入二进制到十六进制的输入时,我得到一个异常“java.lang.StringIndexOutOfBoundsException:String index out of range: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");
}
}
答案 0 :(得分:2)
在binToHex
:
第一个问题是,在bin.substring(i, i+4)
中,i+4
可能超出界限,因为i
会高达bin.length()-1
。
第二个问题是你不知道二进制字符串是否可被4除。你应该用零填充它以使其成为现实。
执行左边距填充后,可以将循环更改为:
for (int i = 0; i < bin.length(); i+=4)
然后bin.substring(i, i+4)
永远不会超出范围。
编辑:我刚注意到你在每次迭代结束时将i递增4。但是,你也在for循环中将i递增1,所以在每次迭代中你总共增加5。
我还建议你使用StringBuilder而不是StringBuffer(你不需要线程安全)。并使用append
方法为其添加字符。并且不要将String.concat用于第二种方法。使用StringBuilder。
在hexToBin
:
您在某些else
之前忘了if
。你有 ”;”在一些条件之后。
例如:if(bin.charAt(i) == 'B');
这意味着将始终执行该条件之后的代码。
正确的实现是(尽管我仍然建议使用StringBuilder):
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");
}
else if(bin.charAt(i) == 'B')
{
binary = binary.concat("1011");
}
else if(bin.charAt(i) == 'C')
{
binary = binary.concat("1100");
}
else if(bin.charAt(i) == 'D')
{
binary = binary.concat("1101");
}
else if(bin.charAt(i) == 'E')
{
binary = binary.concat("1110");
}
else if(bin.charAt(i) == 'F')
{
binary = binary.concat("1111");
}