Multiplayer TCP/IP server & client via Processing and Java

i have problem with this...

input = input.substring(2, input.indexOf("\n")); // <-- ??? what is number: 2 ?

error

Answers

  • Answer ✓

    you tell us, it's your code!

    what is input?

    (substring cuts down a string. the 2 is the start position, the indexOf is the end position (which is itself looking for a newline within the string))

    String str = "example\nstring";
    str = str.substring(2, str.indexOf("\n"));
    println(str);
    

    returns "ample", so it's picking from after the 2nd character until just before the first newline.

    your ArrayIndexOutOfBounds suggests that your input is shorter than 2. debug using:

    println("Input: ", input);
    
  • (that code also breaks if there's no newline in the string. it is actually quite poor code)

  • Check this recent post: https://forum.processing.org/two/discussion/comment/100930/#Comment_100930

    Although this post is related to serial events, they managed the incoming data not in a good way. The key point is to make sure you are not manipulating or accessing null objects. When you apply calls using substring, you are assuming your incoming data has so many delimiters. Don't assume but check first before accessing the data. It will save you lots of headaches. You will need to add some extra code. This is part of the design game.

    Kf

Sign In or Register to comment.