【发布时间】:2012-02-09 11:49:03
【问题描述】:
我编写了一个小程序,其中用户输入分钟,程序显示当前日期和时间 + 用户输入的分钟。
final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, Integer.valueOf(sample.getMinutes()));
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
String dt = sdf.format(cal.getTime());
System.out.println(" Date and time with added Minutes : " + (dateFormat.parse(dt));
示例
private String minutes;
//getter and setter
我遇到了这个异常
java.lang.NumberFormatException:对于输入字符串:“”
我在这里做错了什么? 我应该使用
Integer.parseInt
或
Integer.valueOf(Integer.parseInt(sample.getMinutes())));?
【问题讨论】:
-
什么是
sample?NumberFormatException的发生是因为您有一个空字符串""试图将其解析为数字,但空字符串无法合理地解析为数字。sample.getMinutes()返回一个空字符串。 -
你初始化了样本变量吗?
标签: java numberformatexception