Results 1 to 5 of 5
- 05-07-2009, 03:27 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
[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
- 05-07-2009, 03:30 PM #2
The array is declared as having no dimensions. I think you wantJava Code:int n = 0; Object A[][] = new Object[n][n];
instead.Java Code:A = recieved;
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-07-2009, 03:49 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
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.
- 05-07-2009, 04:24 PM #4
Try a cast
If that doesn't work, using a Collection or some kind of plaintext format to transport the data might be easier.Java Code:A = (Object[][])recieved;
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-07-2009, 04:50 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
[SOLVED] Multi-dimensional arrays
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 05-07-2009, 02:52 PM -
Multi-dimensional array program-help!
By bobmasta5 in forum New To JavaReplies: 13Last Post: 03-15-2009, 07:21 PM -
Multi-dimensional array
By VinTiger in forum New To JavaReplies: 22Last Post: 03-01-2009, 06:51 AM -
Multi dimensional Array
By Preethi in forum New To JavaReplies: 1Last Post: 07-09-2008, 03:34 PM -
Help with array multi-dimensional
By barney in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks