How to print String instead of address in Java?

♀尐吖头ヾ 提交于 2020-12-15 08:37:08

问题


My output is "[B@b42cbf" with no errors.

It should be a string that says "Server Check".

How do I fix my code to put out the string rather than the address?

My code to print the object has been changed several times, but is now as follows.

System.out.println(packet.getMessage().toString());

My packet class is as follows.

import java.io.Serializable;

public class Packet implements Serializable {

    final public short MESSAGE = 0;
    final public short COMMAND = 1;

    private String _ip;
    private short _type;
    private String _source;
    private String _destination;
    private byte[] _message;


    public Packet(String ip, short type, String source, String destination,
            byte[] message) {
        this._ip = ip;
        this._type = type;
        this._source = source;
        this._destination = destination;
        this._message = message;
    }

    public String getIP() {
        return this._ip;
    }

    public Short getType() {
        return this._type;
    }

    public String getSource() {
        return this._source;
    }

    public String getDestination() {
        return this._destination;
    }

    public byte[] getMessage() {
        return this._message;
    }
}

I Send the packet though an ObjectOutputStream and recieve it in an ObjectInputStream. The object is covereted to a packet with (Packet). You can see how this works as follows.

public void sendPacket(Packet packet) throws NoConnection {
        if (this._isConnected) {
            try {
                this._oos.writeObject(packet);
                this._oos.flush();  // Makes packet send
            } catch (Exception e) {
                e.printStackTrace();
                this._isConnected = false;
                throw new NoConnection("No notification of disconnection...");
            }
        } else {
            throw new NoConnection("No connection...");
        }
    }

Here is the listener.

@Override
    public void run() {
        try {
            this._ois = new ObjectInputStream(this._socket.getInputStream());
            Packet packet = (Packet) this._ois.readObject();
            this._listener.addPacket(packet);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

回答1:


[B@b42cbf is what you get when you print a byte array, i.e. binary data.

To get a String from that, you need to know the encoding, and then you can do:

String messageStr = new String(packet.getMessage(), "UTF-8");

Of course, that only works if that data is actually printable data.




回答2:


getMessage() returns a byte array. The toString() method for an array does NOT print its contents. You could make getMessage() return a String instead.




回答3:


This is normal, you are printing the array object as a String.

Use: System.out.println(new String(packet.getMessage());.

That is, build a String out of the bytes in it. And note that this uses the default encoding.



来源:https://stackoverflow.com/questions/8586865/how-to-print-string-instead-of-address-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!