arduino mega 2560 not working properly

In this case, read till you have received everything that you have send before sending again.

Read Serial Input Basics - updated for a general purpose way to read serial ports (examples are for Serial, but can easily be adapted for Serial 1/2/3.

Note that with println(), you add '\r\n' to the text that you send. The second example in linked thread will read till '\n'; you will probably want to strip the '\r' in real life as well; not necessary for this exercise.