1

我有一个日期字符串:

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]);

这是回历月份数组:

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

我究竟做错了什么?

4

1 回答 1

2

您是否有理由需要使用许多不同的字符类型来拆分字符串?

它失败的原因是因为您使用的拆分字符

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

也在月份名称中。

在该行之后放置一个调试点,或者在 dateAr 数组上为每个执行一个并记录结果。这样,您就可以了解它是如何被拆分的。

于 2012-07-12T19:57:52.160 回答