Get Website Learn Coding Tech Story

Java Programming Examples

/* 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

Learn more about InetAddress