2

I am implementing a client server application. from the server i am sending a String array, and now i want to read whats in that string array from my client code. How can i do this;

When i print the value from the client i get the output something like [Ljava.lang.String;@120d10

server:

try {
                    PrintWriter r= (PrintWriter) i.next();

                    String[] s={"f","ff"};
                    r.println(s);

                    r.flush();
                } catch (Exception ex) {

                }

client:

try {
                    while ((somestring= r.readLine()) != null) {
                        //When i print it i get something like [Ljava.lang.String;@120d10


                    }
                } catch (Exception ex) {}

4 Answers 4

3

When you print an array, it call toString() on it first. The default toString() for an array prints the type @ hashCode which is generally useless.

What you intended was something like

String[] arr={"f","ff"};
for(String s: arr)
    r.println(s);
Sign up to request clarification or add additional context in comments.

Comments

2

r.println(object) calls object.toString() to know what to print. The arrays stringTo() method return just that value ([L means you are dealing with an array).

If you want to print all the array, loop it.

for(String  str : s) {
  r.println(str + delimiter);
}

Of course, you will have to find a valid delimiter (one that does not appear inside your strings).

Comments

1

You're trying to println the array of strings, instead of each string in the array. Try something like this on the server:

    try {
                PrintWriter r= (PrintWriter) i.next();

                String[] s={"f","ff"};
                for(String sElement : s)
                {
                    r.println(sElement);
                    r.flush();
                }

            } catch (Exception ex) {

            }

Comments

1

You should consider using ObjectOutputStream / ObjectInputStream instead. Then you can send all kinds of Serializable objects (including arrays) directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.