为什么我的程序崩溃(java)?
我需要使用不同的方法将十六进制转换为十进制。当我输入正确的十六进制数字时,我的程序显示十进制值并表示该数字有效。但是,当我输入不正确的十六进制值时,我的程序崩溃。 这里是我的代码:为什么我的程序崩溃(java)?
import java.io.*;
import java.util.Scanner;
public class pg3a {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
String hex;
char choice = 'y';
boolean isValid = false;
do {
System.out.print("Do you want to enter a hexadecimal number? ");
System.out.print("y or n?: ");
choice = keyboard.next().charAt(0);
switch(choice){
case 'y':
System.out.print("Enter a hexadecimal number: #");
hex = keyboard.next();
hex = hex.toUpperCase();
int hexLength = hex.length();
isValid = valid(hex);
Integer value = Integer.parseInt(hex,16);
System.out.println("The value: " + value);
if (isValid) {
System.out.println(hex + " is valid");
}
break;
case 'n':
System.out.print("Quit");
}
}while (choice != 'n');
}
public static boolean valid (String validString) {
int a = 0;
if (validString.charAt(0) == '-') {
a = 1;
}
for (int i=a; i< validString.length(); i++) {
if (!((validString.charAt(i) >= 'A' && validString.charAt(i) <= 'F')|| (validString.charAt(i) >= 0 && validString.charAt(i) <= 9)))
{
return false;
}
}
return true;
}
public static long convert (String hexValue) {
long decimal = 0;
boolean isNegative = false;
int a = 0;
if (hexValue.charAt(0) == '-') {
isNegative = true;
a = 1;
}
for (int i = a; i<hexValue.length(); i++) {
decimal = decimal*16;
if (hexValue.charAt(i) >= '0' && hexValue.charAt(i) <= '9') {
decimal += hexValue.charAt(i) - '0';
}
else if (hexValue.charAt(i) >= 'a' && hexValue.charAt(i) <= 'f') {
decimal += hexValue.charAt(i) - 'a' + 10;
}
}
if (isNegative == true) {
decimal *= -1;
}
return decimal;
}
}
为什么会崩溃,我怎么能解决这个问题,以便它显示“无效”时需要输入不正确十六进制数字?
如果输入无效的十六进制数字,Integer.parseInt()
将抛出NumberFormatException
。改变你的代码是这样的:
...
isValid = valid(hex);
if (isValid) {
Integer value = Integer.parseInt(hex,16);
System.out.println("The value: " + value);
System.out.println(hex + " is valid");
}
...
当我这样做时,程序不再崩溃(所以谢谢!!!)但现在当我输入一个有效的十六进制数时,它显示为“无效”而不是值。任何想法为什么? – user2805867
检查你的'valid()'方法。 – Axel
添加Integer value = Integer.parseInt(hex,16);
里面if语句和打印无效的else块。
if (isValid) {
Integer value = Integer.parseInt(hex,16);
System.out.println("The value: " + value);
System.out.println(hex + " is valid");
}
else{
System.out.println("invalid");
}
更新时间:
更改您的有效方法如下:
public static boolean valid(String validString) {
int a = 0;
if (validString.charAt(0) == '-') {
a = 1;
}
for (int i = a; i < validString.length(); i++) {
// if (!((validString.charAt(i) >= 'A' && validString.charAt(i) <= 'F') || (validString.charAt(i) >= 0 && validString.charAt(i) <= 9))) {
// return false;
// }
char ch=validString.charAt(i);
if(!(Character.isDigit(ch) || (Character.isLetter(ch) && ((ch-'A')<=5)))){
return false;
}
}
return true;
}
当我这样做时,程序不再崩溃(所以谢谢!!!)但现在当我输入一个有效的十六进制数字时,它显示为“无效”而不是该值。任何想法为什么? – user2805867
您的isValid可能会返回不正确的结果。 – Masudul
你能告诉我你输入了哪个有效值吗? – Masudul
do {
System.out.print("Do you want to enter a hexadecimal number? ");
System.out.print("y or n?: ");
choice = keyboard.next().charAt(0);
int base = 10;
switch(choice)
{
case 'y':
System.out.print("Enter a hexadecimal number: #");
hex = keyboard.next();
hex = hex.toUpperCase(); //I'm not sure if this step is necessary
try {
Integer value = Integer.parseInt(hex, 16);
System.out.println("Valid hex format");
System.out.println("Hex: " + hex);
System.out.println("Decimal: " + value);
}
catch (NumberFormatException e) {
System.out.println("Invalid hex format");
System.out.println("Input: " + hex);
}
break;
case 'n':
System.out.print("Quit");
break
}
} while (choice != 'n');
现在你可以删除所有的辅助方法
后的堆栈跟踪,请。 – Fildor
你不允许负的十六进制值 Fildor
为什么要做这一切?把一个try-catch放在parseInt调用的周围,并且在[OPs previous question]中建议它可以捕获'NumberFormatException' – Ron