Wed Oct 27 2021

Hostname to IP

File Name: hostname-to-ip.java

/* Get IP Address from Hostname */
import java.util.Scanner;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class hostToIP {
	public static void main(String[] args) {
		InetAddress address = null;
		try {
			System.out.println("Enter a domain/host name:");
			Scanner input = new Scanner(System.in);

			/* Get host name */
			address = InetAddress.getByName(input.nextLine());
		}
		catch (UnknownHostException e) {
			System.exit(2);
		}

		/* Display hostname and IP address of the host */
		System.out.println("Hostname: "+address.getHostName() +"\nIP Address: "+address.getHostAddress());
		System.exit(0);
	}
}



/* Output */
Enter a domain/host name:
www.geekboots.com

Hostname: www.geekboots.com
IP Address: 103.21.58.244
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.