Results 1 to 5 of 5
  1. #1
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default [SOLVED] Multi-dimensional Array further help

    Hi

    I have managed to send the multi-dimensional array to the client, the only problem is I am now trying to make use of the sent Object as follows:-

    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
    import java.nio.*;
    import java.util.*;

    public class TCPclient
    {
    public static void main(String[] args)

    {

    // Declare client socket
    Socket clientSocket = null;
    Scanner myScanner = new Scanner(System.in);
    // Declare output stream and string to send to server
    DataOutputStream os = null;
    int request;
    int n = 0;
    Object A[][] = new Object[n][n];


    // Declare input stream from server and string to store input received from server
    BufferedReader is = null;
    int responseLine;
    int b;
    InputStream in;
    ObjectInputStream recieve;
    Object recieved;
    Object recievedB;
    Object c;


    // Create a socket on port 1024
    //Stream header
    try
    {
    clientSocket = new Socket("localhost", 1024);
    os = new DataOutputStream(clientSocket.getOutputStream());
    is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));






    }
    catch (UnknownHostException e)
    {
    System.err.println("Don't know about host: hostname");
    }
    catch (IOException e)
    {
    System.err.println("Couldn't get I/O for the connection to: hostname");
    }

    // Write data to the socket
    if (clientSocket != null && os != null && is != null)
    {


    try
    {
    request = 0;
    os.writeInt(request);
    in = clientSocket.getInputStream();
    recievedFrom = new ObjectInputStream(in);

    while(request != 1)
    {
    responseLine = is.read();
    b = responseLine;
    System.out.println("Client b " + b + "\n");
    request++;
    recieved = recieveFrom.readObject();
    recievedB = recieveFrom.readObject();
    System.out.println(recieved);
    System.out.println(recievedB);
    A[n][n] = recieved;

    }//end while

    // Close the input/output streams and socket

    os.flush();
    is.close();
    clientSocket.close();
    }//end try
    catch (UnknownHostException e)
    {
    System.err.println("Trying to connect to unknown host: " + e);
    }
    catch (IOException e)
    {
    System.err.println("IOException: " + e);
    }
    catch(ClassNotFoundException e)
    {
    System.out.println(e);
    }
    }//end if

    }
    }

    The variable recieved holds the contents of readObject(), this recieves the Object from the Server. I have created the variable A[n][n] which is to store the result of recieved. I have implemented A[n][n] = recieved inside of the while() loop, but get the following exception message
    [[Ljava.lang.Object;@14318bb
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at TCPclient.main(TCPclient.java:83)

    therefore I know that the Object is being recieved, but the A[n][n] = recieved; is not liked, it will compile, but the Exception is thrown. I am trying to get the value of A[n][n] for use outside of the loop for further calculations. An advice on how I could do this would be apprciated.

    theLinuxGuy

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Java Code:
    int n = 0;
    Object A[][] = new Object[n][n];
    The array is declared as having no dimensions. I think you want
    Java Code:
    A = recieved;
    instead.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default

    Hi
    OrangeDog

    That did not work, I entered A = recieved; and got

    TCPclient.java:83: incompatible types
    found : java.lang.Object
    required: java.lang.Object[][]
    A = recieved;
    The Object being recieved does have dimensions.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Try a cast
    Java Code:
    A = (Object[][])recieved;
    If that doesn't work, using a Collection or some kind of plaintext format to transport the data might be easier.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default

    Hi OrangeDog

    Thanks,
    A = (Object[][])recieved;
    compiles and runs fine


    Thanks again

    theLinuxGuy

Similar Threads

  1. [SOLVED] Multi-dimensional arrays
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2009, 02:52 PM
  2. Multi-dimensional array program-help!
    By bobmasta5 in forum New To Java
    Replies: 13
    Last Post: 03-15-2009, 07:21 PM
  3. Multi-dimensional array
    By VinTiger in forum New To Java
    Replies: 22
    Last Post: 03-01-2009, 06:51 AM
  4. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 03:34 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •