0

Im using a hashtable to save filename (string) and InetAddress

Hashtable <String , InetAddress > file_location = new Hashtable <String , InetAddress >(); 

and im using this to retrieve the address, but im only getting a null value returned

file_location.put("ABD_9158" , IPAddress); //IPAdress is of InetAddress type

 InetAddress n = file_location.get("ABD_9158");

        System.out.println(n);

tried changing n to a string but , havent been able to find away my question, how do retrieve the ipaddress ?

Amr Hamada
  • 63
  • 1
  • 2
  • 8

1 Answers1

0

This is a small program, to check what you want. Same piece of code, what you have shared, it is working fine.

 public static void main(String args[]) {
        Hashtable<String, InetAddress> fileLocation = new Hashtable<String, InetAddress>();
        InetAddress addr;
        try {
            addr = InetAddress.getByName("127.0.0.1");
            fileLocation.put("ABD_9158", addr); // IPAdress is of InetAddress type
            InetAddress n = fileLocation.get("ABD_9158");
            System.out.println(n);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

Output

/127.0.0.1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • yes actually when i used the the variable instead of "ABD_9158" i got the address return correctly , do u know why this might be happening. i am a hundred percent sure im writing correctly and matching it to the variable? – Amr Hamada Nov 20 '14 at 05:42
  • i compared both strings, the variable and the string "ABD_9158" and for some reason theyre not the same – Amr Hamada Nov 20 '14 at 05:51
  • @AmrHamada are you using `==` or `equals` to compare, the later will give you correct result, incase you are missing on that – Ankur Singhal Nov 20 '14 at 05:54
  • no i was using `equals` when that didnt work i also used `equalsIgnoreCase`, if this helps im actually sending the file name through a datagram socket and recieving the file with this line `String file = new String( receivePacket.getData());` – Amr Hamada Nov 20 '14 at 06:02