These code samples are extremely long and very similar, so it's painfully difficult to pin down what the actual problem is.
However one obvious mistake that does stand out is in these lines from version 2:
if(selectedposition.length>1 || selectedposition[0].equals("$GPGGA"))
and
else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))
According to your version 1 code, the algorithm should check that selectedposition.length>1
, and if it is, then go and try to do stuff. If it isn't, don't do anything.
But in the version 2 code listed above, the block
else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))
will NEVER be called unless the input is exactly one character long.
To give the original functionality, you need to change both of those ||
operators to &&
. That way it will check both cases as required. That is, use:
if(selectedposition.length>1 && selectedposition[0].equals("$GPGGA"))
and
else if(selectedposition.length>1 && selectedposition[0].equals("$GPRMC"))