Vai al contenuto

[Java] unchecked call con Hashtable...


BrK

Messaggi raccomandati

Ciao, stò facendo un mini programmino in java per testare una cosa eppure ho qualche problema in fase di compilazione (nulla di che, un warning, eppure mi stà facendo andare in bestia)...

Qualcuno ha qualche idea?

Ecco il codice:

//server di comunicazione

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.DateFormat;

class server {
public static void main (String args[]) throws Exception {
	ServerSocket server = null;
	Socket client = null;
	ThreadTCP t;
	Hashtable hashtbl = new Hashtable();

	try
	{
		server = new ServerSocket(7000);
	}
	catch (IOException e){}
	while(true)
	{
		client = server.accept();
		t = new ThreadTCP(client, hashtbl);
		t.start();
	}
}
}

//rendere multithread
class ThreadTCP extends Thread {

private Socket thread_client = null;
//User tmp = null;
private Date now = new Date();
private Hashtable onuser = new Hashtable();

public ThreadTCP(Socket client, Hashtable hashtbl)
{
	this.thread_client = client;
	this.onuser = hashtbl;
}

public void run()
{
	try
	{
		String strng_client, frase_server;
		InetAddress tmpip = null;
		String tmp = new String("testo a caso");
		BufferedReader from_client = new BufferedReader(new InputStreamReader(thread_client.getInputStream()));
		strng_client = from_client.readLine();
		tmpip = thread_client.getInetAddress();
		System.out.println(tmpip + " connesso " + DateFormat.getTimeInstance(DateFormat.SHORT).format(now));
		if(onuser.contains(tmpip)){
			System.out.println("gia connesso");	
		}
		else {
			System.out.println("connettiti");
			onuser.put(tmp, tmpip);
		}
		frase_server = strng_client.toUpperCase();
		DataOutputStream server_out = new DataOutputStream(thread_client.getOutputStream());
		server_out.writeBytes(frase_server + '\n');
	}
	catch (IOException e) {}
}
}

Il warning è il seguente:

server.java:82: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Hashtable
                               onuser.put(tmp, tmpip);
                                         ^
1 warning

Qualche idea???

Link al commento
Condividi su altri siti

  • 2 settimane dopo...

Apple utilizza la versione 5 di Java che ha introdotto il concetto di "generics", ossia permette di specificare il tipo di dati da inserire in una collection in modo da controllare in fase di compilazione che tutti gli oggetti immessi nella collection siano del tipo desiderato.

Il warning ti avvisa che, non avendo specificato un tipo, non può fare il controllo. Se vuoi aggiungere il controllo e far sparire l'errore devi dichiarare la collection così:

Hashtable<String, InetAddress> onuser = new HashTable<String, InetAddress>();

ricorda solo che questo tipo di dichiarazione funziona solo a partire dalla JVM 5.

Ciao!

Link al commento
Condividi su altri siti

Archiviato

Questa discussione è archiviata e chiusa a future risposte.

×
×
  • Crea Nuovo...