Reputation: 1033
The code I've made automatically returns the host name.
But instead of returning my machine's host name every time. I want to checkup on other machines as well (for testing purpose).
By that I mean, every time I call the method, it'll ask me to enter an IP address, and then return me the host name of the address I've entered.
For example:
Here's my code:
import java.net.InetAddress;
public class Search
{
public String findH(String x) throws Exception {
InetAddress a = InetAddress.getLocalHost();
String s = a.getHostName();
System.out.println("Host Name is: " + a.HostName());
return x;
}
}
Thanks in advance. I know my description isn't the best, but let me know if there's any ambiguity.
Upvotes: 2
Views: 3328
Reputation: 7501
Try
public String findH(String x) throws Exception {
InetAddress addr = InetAddress.getByName(x);
return addr.getHostName();
}
Upvotes: 2
Reputation: 55720
Instead of calling InetAddress.getLocalHost()
you want to create the address from x
:
InetAddress a = InetAddress.getByName(x);
The rest of your code would stay the same..
oh, and you probably want to return a.getHostName()
not x
Upvotes: 0