Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的十六进制字符串到整数解析异常处理_Java_String_Parsing_Exception_Integer - Fatal编程技术网

java中的十六进制字符串到整数解析异常处理

java中的十六进制字符串到整数解析异常处理,java,string,parsing,exception,integer,Java,String,Parsing,Exception,Integer,我需要将字符串十六进制值解析为整数值。像这样: String hex = "2A"; //The answer is 42 int intValue = Integer.parseInt(hex, 16); 但当我插入一个不正确的十六进制值(例如“LL”)时,我会得到java.lang.NumberFormatException:对于输入字符串:“LL”如何避免它(例如返回0) 将其包含在try-catch块中。这就是工作原理:- 将其封装在一个try-catch块中。这就是工作原理:-

我需要将字符串十六进制值解析为整数值。像这样:

String hex = "2A"; //The answer is 42  
int intValue = Integer.parseInt(hex, 16);

但当我插入一个不正确的十六进制值(例如“LL”)时,我会得到
java.lang.NumberFormatException:对于输入字符串:“LL”
如何避免它(例如返回0)

将其包含在try-catch块中。这就是工作原理:-


将其封装在一个try-catch块中。这就是工作原理:-

对于输入字符串:“LL”如何避免它(例如返回0)


只需捕获异常并将zero分配给intvalue

int intValue;
try {
String hex = "2A"; //The answer is 42  
intValue = Integer.parseInt(hex, 16);
}
catch(NumberFormatException ex){
  System.out.println("Wrong Input"); // just to be more expressive
 invalue=0;
}
对于输入字符串:“LL”如何避免它(例如返回0)


只需捕获异常并将zero分配给intvalue

int intValue;
try {
String hex = "2A"; //The answer is 42  
intValue = Integer.parseInt(hex, 16);
}
catch(NumberFormatException ex){
  System.out.println("Wrong Input"); // just to be more expressive
 invalue=0;
}
您可以捕获异常并返回零

public static int parseHexInt(String hex) {
    try {
        return Integer.parseInt(hex, 16);
    } catch (NumberFormatException e) {
        return 0;
    }
}
但是,我建议重新评估您的方法,因为0也是一个有效的十六进制数,并不表示无效的输入,例如
“LL”
,您可以捕获异常并返回零

public static int parseHexInt(String hex) {
    try {
        return Integer.parseInt(hex, 16);
    } catch (NumberFormatException e) {
        return 0;
    }
}
但是,我建议重新评估您的方法,因为0也是一个有效的十六进制数,并不表示无效的输入,例如
“LL”

如何避免它(例如返回0)

使用一个简单的方法,
在发生
NumberFormatException
时返回0

public int getHexValue(String hex){

    int result = 0;

    try {
        result = Integer.parseInt(hex, 16);
     } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return result;
}
如何避免它(例如返回0)

使用一个简单的方法,
在发生
NumberFormatException
时返回0

public int getHexValue(String hex){

    int result = 0;

    try {
        result = Integer.parseInt(hex, 16);
     } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return result;
}

只需捕获异常并设置默认值。但是,您需要在
try
块之外声明变量

int intValue;
try {
    intValue = Integer.parseInt(hex, 16);
} catch (NumberFormatException e) {
    intValue = 0;
}
如果需要使用初始值设定项表达式设置值(例如,对于
final
变量),则必须将逻辑打包到一个方法中:

public int parseHex(String hex) {
    try {
        return Integer.parseInt(hex, 16);
    } catch (NumberFormatException e) {
        return 0;
    }
}

// elsewhere...
final int intValue = parseHex(hex);

只需捕获异常并设置默认值。但是,您需要在
try
块之外声明变量

int intValue;
try {
    intValue = Integer.parseInt(hex, 16);
} catch (NumberFormatException e) {
    intValue = 0;
}
如果需要使用初始值设定项表达式设置值(例如,对于
final
变量),则必须将逻辑打包到一个方法中:

public int parseHex(String hex) {
    try {
        return Integer.parseInt(hex, 16);
    } catch (NumberFormatException e) {
        return 0;
    }
}

// elsewhere...
final int intValue = parseHex(hex);

请注意,您必须在try块之外声明变量
intValue
。范围很重要。请注意,您必须在try块之外声明变量
intValue
。范围很重要。