-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHostNameToIP.java
More file actions
22 lines (19 loc) · 1.14 KB
/
HostNameToIP.java
File metadata and controls
22 lines (19 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// This program accepts a hostname and displays its IP address.
import java.net.InetAddress; // Import the InetAddress class for handling IP addresses
import java.net.UnknownHostException; // Import the UnknownHostException class for handling errors
import java.util.Scanner; // Import the Scanner class for user input
public class HostNameToIP {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object for reading user input
System.out.print("Enter hostname: "); // Prompt the user to enter a hostname
String hostname = scanner.nextLine(); // Read the hostname entered by the user
try {
// Get the IP address of the host
InetAddress address = InetAddress.getByName(hostname); // Resolve the hostname to an IP address
System.out.println("IP address: " + address.getHostAddress()); // Print the IP address
} catch (UnknownHostException e) {
// Handle the case where the hostname could not be resolved
System.out.println("Could not find IP address for: " + hostname); // Print an error message
}
}
}