Why do i get for an Integer.parseInt(String) an numberformatexception?

Viewed 113

I feel like the solution is very simple but I am right now staring at it for 10 Minutes and i cannot find any errors in my code. Seriously.

Whereas eventDateStart is an integer. Integer normally do take 8 characters for string to int conversions and i don't see why Java cannot converse it I am like ... ugh??

Code is producing NumberFormatException:

String rruleMonthday = "-1";

//where ar rrule is sth like --> RRULE:FREQ=MONTHLY;BYMONTHDAY=29
int firstMonthday = (rrule.indexOf("BYMONTHDAY=") + 11);

try {
rruleMonthday = rrule.substring(firstMonthday, rrule.indexOf(";", firstMonthday));
} catch (Exception ex) {
 rruleMonthday = rrule.substring(firstMonthday, rrule.indexOf("\n", firstMonthday));
}

eventDateStart = eventDateStart.substring(0, 6) + rruleMonthday;

System.out.println("eventDateStart: ." + eventDateStart + ".");


System.out.println("2: " + Integer.parseInt(eventDateStart)); //this Integer.parseInt conversion is printing numberformatexception 


output:

eventDateStart: .20200329.

java.lang.NumberFormatException: For input string: "20200329"
1 Answers

As per ASCII table the sequence 50,48,50,48,48,51,50,57 is the 20200329 value. In your case it's followed by a carriage return symbol 13.

Most likely you are using Windows where the line end sequence is \r\n. Instead of \n use System.lineSeparator() to make your code platform independent:

} catch (Exception ex) {
    rruleMonthday = rrule.substring(firstMonthday, 
            rrule.indexOf(System.lineSeparator(), firstMonthday));
}
Related