【问题标题】:NumberFormatException: unable to parse '' as integerNumberFormatException:无法将“”解析为整数
【发布时间】:2012-07-12 19:50:46
【问题描述】:

我有一个日期字符串:

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

我做错了什么?

【问题讨论】:

    标签: java android arrays parsing numberformatexception


    【解决方案1】:

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

    它失败的原因是因为你使用的分割字符

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

    也在月份名称中。

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

    【讨论】:

    • 感谢您指出。我删除了s+ 的空格,它起作用了。但是,如果我想在标签中拆分分隔符- 的字符串但还要在月份名称中显示它,还有什么其他的解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    相关资源
    最近更新 更多