NumberFormatException:无法解析''作为整数

时间:2012-07-12 19:50:46

标签: java android arrays parsing numberformatexception

我有一个日期字符串:

gridcell.setTag(theday + "-" + themonth + "-" + theyear + "|" + hijri_day + "-" + hijri_month + " ("+ hijri_monthno +") " + hijri_year);

..如果日期有事件,我会在按钮点击时传递给另一个类:

String date_month_year = (String) view.getTag();
if (isHoliday(d, m, y))
{
    Intent i = new Intent(view.getContext(), Events.class);
    i.putExtra("date_string", date_month_year);
    startActivity(i);
}

在Events.class中,我得到了参数:

Intent intent = getIntent();
String date_string = intent.getStringExtra("date_string");

date_view = (TextView) this.findViewById(R.id.hijridate);
eventdetails = (TextView) this.findViewById(R.id.eventdetails);
date_view.setText(date_string);


String[] dateAr = date_string.split("-|\\||\\(|\\)|\\s+");
m = Integer.parseInt(dateAr[6]);
d = Integer.parseInt(dateAr[3]);
y = Integer.parseInt(dateAr[8]);

这是Hijri月份数组:

private String months[] = {"Muharram","Safar","Rabi-al Awwal","Rabi-al Thani","Jumada al-Ula","Jumada al-Thani","Rajab","Sha\'ban","Ramadhan","Shawwal","Dhul Qa\'dah","Dhul Hijjah"};

我遇到的问题是,当它是一个单词月份名称(即Muharram,Safar,Rajab等)时,一切顺利。但是,如果它是带有空格或破折号的单词(即Rabi-al awwal,Dhul Hijjah),则会抛出错误:NumberFormatException: unable to parse '' as integerNumberFormatException: unable to parse 'al' as integer

我做错了什么?

1 个答案:

答案 0 :(得分:2)

是否有理由需要使用大量不同的字符类型来分割字符串?

它失败的原因是你在

使用的分裂字符
  

String [] dateAr = data_string.split(“ - | \ || \(| \)| \ s +”);

也是月份名称。

在该行之后放置一个调试点,或者对dateAr数组执行一个调试,并记录结果。这样,你就可以理解它是如何分裂的。