I have a date string:
gridcell.setTag(theday + "-" + themonth + "-" + theyear + "|" + hijri_day + "-" + hijri_month + " ("+ hijri_monthno +") " + hijri_year);
..which I pass on to another class on button click if the date has an event:
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);
}
In the Events.class, I get the parameters:
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]);
This is the Hijri month array:
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"};
The problem I’m having is that when it’s a one word month name (i.e. Muharram, Safar, Rajab, etc. .) everything works smoothly. However, if it’s a word with a space or a dash (i.e. Rabi-al awwal, Dhul Hijjah), it throws the error: NumberFormatException: unable to parse '' as integer or NumberFormatException: unable to parse 'al' as integer
What am I doing wrong?
Is there a reason you need to use a lot of different character types to split the string on?
The reason it’s failing is because the split characters your using at
are also in the month name.
put a debug point after that line, or do a for each over the dateAr array and log the results. That way, you can understand how it’s being split.