Search code examples
javastringhashtableinetaddress

converting InetAddress to string


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 ?


Solution

  • 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