Sometimes, the hardest part of debugging isn't finding the bug. It's actually paying attention to what the error message is telling you.
Today, I was working on a simple Java task: splitting a string like name=Kwame into a key and a value.
The idea was straightforward:
Find the position of = .
Extract everything before it as the key.
Extract everything after it as the value.
Here's the code:
String text = "name=Kwame" ;
int index = text . indexOf ( '=' );
String key = text . substring ( 0 , index ); String value = text . substring ( index + 1 );
System . out . println ( key ); System . out . println ( value );
Enter fullscreen mode
Exit fullscreen mode
Expected output:
name Kwame
Enter fullscreen mode
Exit fullscreen mode
Simple enough, right?
Where I went wrong
My first implementation looked like this:
String value = text .…