3

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"
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Where does the value for `eventDateStart` come from? I'd try using a debugger to inspect values instead of `println` – Phil Mar 29 '20 at 22:02
  • 1
    What is the return value of `eventDateStart.length()`? – Progman Mar 29 '20 at 22:06
  • 2
    You most likely have unprintable characters in the `20200329`. Post what `System.out.println(eventDateStart.codePoints().mapToObj(String::valueOf).collect(Collectors.joining(",")));` will print. – Karol Dowbecki Mar 29 '20 at 22:06
  • : eventDateStart.length(): 9 and @karol: 50,48,50,48,48,51,50,57,13 i dont yet see what the problem could lie at? – Trendy Jewellery Mar 29 '20 at 22:09
  • 1
    `13` is a carriage return. Try [trimming your string](https://stackoverflow.com/questions/7454330/how-to-remove-newlines-from-beginning-and-end-of-a-string-java) – Phil Mar 29 '20 at 22:10
  • .. i have even put an . and couldn't see the new line. Damn How could I make java trim it without the new line? – Trendy Jewellery Mar 29 '20 at 22:11
  • Thanks this solved my "simple" problem :') – Trendy Jewellery Mar 29 '20 at 22:20

1 Answers1

2

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));
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111