how to call a matrix variable into a method

117 views Asked by At

i'm learning about socket programming but i have a little problem with this matrix. I know that is a stupid problem but i don't know how to resolve it

void data(){
    String[][] serv = { {"url", "url ip"}, {"url", "url ip"}, {"url", "url ip"} };
    System.out.println(server [1] [0]);
}

void process(String[][] serv){
    int i = 0;
    System.out.println("Processing...\n");
    try{
        for (i=0; i<3; i++) {

            System.out.println(serv [i] [0]);
            if (request.equals(serv [i] [0])) {
                response = serv [i] [1];
            }
        }
    }
    catch(Exception ex){
        System.out.println("Non trovato");
    }
}
public static void main(String[] args) throws IOException{
    final int port = 6000;
    ServerSocket listenSock = new ServerSocket(port);
    System.out.println("Server avviato. In ascolto su: " + listenSock.getLocalPort() + "\n");
    while(true){
        Socket socket = listenSock.accept();
        System.out.println("Stabilita connessione con: "+socket.getPort()+"\n");
        TCPServer server = new TCPServer(socket);
        server.getRequest();
        server.data();
        server.process();
        server.sendResponse();
        server.close();     
    }
}

when i compile javac give me this message

TCPServer.java:88: error: method process in class TCPServer cannot be applied to given types; server.process(); ^ required: String[][] found: no arguments reason: actual and formal argument lists differ in length any ideas to resolve it?

2

There are 2 answers

0
Eran On BEST ANSWER

First of all, your array must be an instance variable and not local to the data() method :

String[][] serv;
void data(){
    serv = { {"url", "url ip"}, {"url", "url ip"}, {"url", "url ip"} };
    System.out.println(server [1] [0]);
}

Then you can use it directly in the process method without passing anything :

void process() {
   ...

Your main method can remain unchanged.

Another alternative is to return the array from the data() method and pass it to the process() method :

String[][] data(){
    String[][] serv = { {"url", "url ip"}, {"url", "url ip"}, {"url", "url ip"} };
    System.out.println(serv [1] [0]);
    return serv;
}

And in your main method :

    server.process(server.data());

In this solution you don't change anything in the process() method.

0
Mena On

Your server.process() method takes a double-dimension array of String.

In your main method, you invoke it without arguments, hence the error.