北峰高炮基地宿舍图片:JAVA编程

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 14:23:11
用来将作为命令行参数输入的值转换为数字,如果输入的值无法转换为数字,则程序应显示相应的错误消息,要求通过异常处理方法解决

public class Conver
{
public static void main(String[] args)
{
for (int i=0;i<args.length;i++)
{
try{
double result=Double.valueOf(args[i]).doubleValue();
System.out.println(result);
}catch(NumberFormatException nfe){
//TODO: 这里添加异常处理代码。
}
}
}
}

s为要进行转换的字符串
字符串型转换成各种数字类型:

try{
String s = "169";
byte b = Byte.parseByte( s );
short t = Short.parseShort( s );
int i = Integer.parseInt( s );
long l = Long.parseLong( s );
Float f = Float.parseFloat( s );
Double d = Double.parseDouble( s );

}catch(Exception e){

e.printStackTrace(); //输出错误信息

}