String[] IpMask; Task = in.readUTF(); IpMask = Task.split("-"); ThisServerFrame.Tip.setText(IpMask[0].toString()); ThisServerFrame.Tmask.setText(IpMask[1].toString()); 

I get a line on the socket, I want to splice it up into several lines. Indicates that the length of the final array of the parsed string is 2, but when accessed

 ThisServerFrame.Tmask.setText(IpMask[1].toString()); 

I get to go beyond the array.

  • The question is, why do I get an exception or how to organize a competent approach to reading the elements of an array? - void
  • Why in the array of length 2 I get an error when accessing the element with the index 1 - alex91
  • Are you sure you get an exception for IpMask[1] ? Can setText try to write the wrong data somewhere? - void
  • He writes to the textbox from the array, the error is in the array - alex91
  • Well, before asking for the error log, what is the printout of the IpMask[1] element? - void

3 answers 3

Obviously, the array contains only one element. The problem is that you read from the socket.

Note that for the string "zzz-" split returns an array of one element, and for "-" - an empty array.

  • But my size of the resulting array is 2, and from it I read the element with index 1. Outside of working with sockets, it works with a bang - alex91
  • The fact that, outside of working with sockets, it works with a bang, only proves my statement. There is no magic. - a_gura
  • And what is the difference between the line passed through the network from the one that did not do this? 444-454 is the same string. And in the case of a socket, it successfully reached the whole. I'm trying to break it - alex91
  • @ Cookie Milk Are you sure that your string is "all gone"? Did you output the contents of the string to the log / console? Or maybe they watched the string value in the debugger? Or maybe you edit one source code, and compile and run another. - a_gura
  • 3
    @ Cookie Milk, you claim that System.out.println ("" + IpMask.length); prints the number 2 and when accessing IpMask [1] the program crashes? - avp

Emm ... Sockets are a very slow thing, as practice shows. There is a possibility that the socket still accepts data (it has not yet received everything), and you are already pulling the buffer. Of course, the buffer will return bytes, but not in the amount you expect. Another variant of this problem is that the program rarely jerks the socket buffer, and the user gets glued packets.

There are several options:

  1. Structure the packet in such a way that the first 2 or 4 bytes indicate the length of the entire packet. In the loop, wait for 2/4 bytes to appear in the buffer. Read them. In the loop, wait for the N bytes read from the previous item to appear in the buffer, then pull the desired N number of bytes. Next to them will again be the dimension of the next package.
  2. If there are packets of only one format, and always of fixed length, you can simply wait for the required number of bytes in the buffer, and then read the required number of bytes (not more). The option is not the best, is lazy.
  3. You can make a large buffer in advance, where the FIFO bytes from the socket buffer are flown, and you need to pull the required number of bytes from this stack. The option is not the best - cycling, but met several times.

PS: To make sure of the received, after

 IpMask = Task.split("-"); 

call:

 System.out.println(Task + ", splits for " + IpMask.Length + "parts"); 

and see what happens.

  • The previous similar proposal to confirm that IpMask.Length displays the value 2 in the console, the questioner did not give an answer. - void
  • four
    Apparently, the author simply scored ... :) Or solved the problem in silence, with the greatest depression and shame, which he was not allowed to report on the results. - Evgeny Karpov
  • 2
    I did not score) Sorry, it was worth telling you are right) The problem was solved, it was as often happens - I don’t know what :) When she disappeared, I didn’t remember what I changed, because I tried a bunch of options. In my memory there is a variant with an incorrectly submitted string: (I apologize to the community for such questions. - alex91

Use collections. You can never guarantee that a string coming from outside will be 2+ in size.

Information for thought here

  • Excuse me, but how are the collections related and the size of the incoming string? - Evgeny Karpov
  • while it is easier to handle the output outside the array and not to go beyond the boundary. And in general, it is more bold and safe than using arrays. I cannot say anything about the size of the incoming string, since the question is very vaguely formulated. - Artemis
  • @Artemis Well, this is all correct - competent implementation of the approach in the code to the elements of the array when reading or setting values ​​gives a plus to karma and generally speaks about the professionalism of the programmer. But the simple fact is that the emphasis of the author of the question was focused on why he received an exception if he blindly addressed an element of the array (which, perhaps, could be outside), knowing in advance that it should be, in While the array element was not initialized due to the fact that the data did not “arrive” before it got to the split . And this is another opera. - void
  • As it is twisted) But I simply cannot even assume that arrives after a call of in.readUTF (); Therefore, the only adequate way to avoid mistakes is to think in advance what to do if something still does not arrive. Otherwise, you need to formulate the question pralno - Artemis
  • @Artemis, think in advance what to do ... that's right. A collection or just an array - it does not matter. If a person is not able to program, then nothing will help. Judging by the reaction (or rather, its absence) to your question, the author is exactly that. - avp