Hello guys, today, I am going to learn y'all well-nigh 1 interesting degree from java.net package, the InetAddress. If y'all accept never used this degree before, allow me country y'all that it's an abstraction to stand upwards for an Internet address in addition to it encapsulates both hostname in addition to IP address. Since converting an IP address to hostname or hostname to IP address is a really mutual requirement, Java developer should know well-nigh this class, and, inwards full general well-nigh java.net package, which contains much other useful degree for networking in addition to client-server applications. So, what is the best way to acquire novel APIs similar java.net? Well, past times writing pocket-size programs similar this, which has a pocket-size focus in addition to allow y'all explore things. They are super useful in addition to that's how I accept learned the bulk of Java API.
In this example, I accept used main() method to execute the programme but if y'all desire y'all tin too start writing JUnit examine every bit suggested inwards Test Driven for exploring Regular Expression APIs similar Matcher in addition to Pattern classes in addition to methods like find(). If y'all haven't read that book, brand an attempt to read this weekend, it's an awesome mass in addition to y'all volition live on a amend Java developer afterward reading that book.
Anyway, coming dorsum to the occupation inwards hand, y'all involve to write a Java programme to convert hostname to IP address inwards Java. Though y'all tin hardcode the hostname for testing purpose, I accept made this programme to a greater extent than interactive, which agency y'all tin run this programme in addition to acquire inwards the hostname from the ascendancy prompt.
For Beginners, I recommend this approach because that interactiveness gives the feeling that something is running in addition to working in addition to it too allows y'all to examine past times giving random input in addition to getting immediate feedback.
Though, for Intermediate programmers, I recommend writing JUnit tests alongside predefined input every bit that's how professional person Java developer write code. Though, if y'all don't know much well-nigh JUnit, I propose y'all bring together a course of teaching like Learn Unit Testing alongside JUnit & Mockito inwards thirty Steps on Udemy. That's volition too brand y'all a amend Java developer.
I am certain y'all know how to create that but if y'all don't allow me country y'all that y'all tin job the Scanner degree from java.util bundle to read the user input from the ascendancy line. This degree too does a lot to a greater extent than than only reading input, it tin too convert the input into the information type y'all want.
For example, if y'all acquire inwards "111" on a console than it tin read it every bit a String or every bit an Integer. In this case, nosotros don't involve to create anything exceptional every bit hostnames are going to live on String in addition to Scanner.nextLine() returns String.
Now, coming dorsum to printing the output into the console, y'all tin job patently quondam System.out.println() method, the kickoff method I learned on Java. Btw, if y'all don't much well-nigh this basic concepts thus I too propose y'all become through a comprehensive Java course of teaching similar The Complete Java MasterClass to acquire them inwards a structured way. It's too most up-to-date degree in addition to updated for Java 12.
And, hither is our consummate Java programme to convert hostname to IP address, y'all tin run it either on the ascendancy draw or on your favorite ides similar Eclipse, IntellJIDEA or NetBeans.
Java Program to convert hostname to IP Address
That's all well-nigh how to abide by the IP address from hostname inwards Java. This variety of pocket-size programs volition assistance y'all to acquire in addition to empathize JDK API better. As y'all accept learned around of the key classes from java.net bundle inwards this programme similar InetAddress which contains both hostname in addition to IP Address in addition to abstraction to stand upwards for an network address.
Further Reading
The Complete Java MasterClass
Java Fundamentals: Input/Output
Java Network Programming - fourth Edition
TCP/IP in addition to Networking Fundamentals for information technology Pros
Thanks for reading this article thus far. If y'all similar this article thus delight portion alongside your friends in addition to colleagues. If y'all accept whatsoever upshot on agreement this programme or running it from the ascendancy draw or Eclipse, delight driblet a regime annotation in addition to I'll attempt my best to assistance you.
In this example, I accept used main() method to execute the programme but if y'all desire y'all tin too start writing JUnit examine every bit suggested inwards Test Driven for exploring Regular Expression APIs similar Matcher in addition to Pattern classes in addition to methods like find(). If y'all haven't read that book, brand an attempt to read this weekend, it's an awesome mass in addition to y'all volition live on a amend Java developer afterward reading that book.
Anyway, coming dorsum to the occupation inwards hand, y'all involve to write a Java programme to convert hostname to IP address inwards Java. Though y'all tin hardcode the hostname for testing purpose, I accept made this programme to a greater extent than interactive, which agency y'all tin run this programme in addition to acquire inwards the hostname from the ascendancy prompt.
For Beginners, I recommend this approach because that interactiveness gives the feeling that something is running in addition to working in addition to it too allows y'all to examine past times giving random input in addition to getting immediate feedback.
Though, for Intermediate programmers, I recommend writing JUnit tests alongside predefined input every bit that's how professional person Java developer write code. Though, if y'all don't know much well-nigh JUnit, I propose y'all bring together a course of teaching like Learn Unit Testing alongside JUnit & Mockito inwards thirty Steps on Udemy. That's volition too brand y'all a amend Java developer.
How to Convert Hostname to IP Address inwards Java
Now that y'all know that y'all tin job InetAddress degree from java.net bundle to convert hostname to IP address, let's write our Java program. Apart from that bit, nosotros too involve to know how to acquire user input from the ascendancy draw or console in addition to how to impress values dorsum to console.I am certain y'all know how to create that but if y'all don't allow me country y'all that y'all tin job the Scanner degree from java.util bundle to read the user input from the ascendancy line. This degree too does a lot to a greater extent than than only reading input, it tin too convert the input into the information type y'all want.
For example, if y'all acquire inwards "111" on a console than it tin read it every bit a String or every bit an Integer. In this case, nosotros don't involve to create anything exceptional every bit hostnames are going to live on String in addition to Scanner.nextLine() returns String.
Now, coming dorsum to printing the output into the console, y'all tin job patently quondam System.out.println() method, the kickoff method I learned on Java. Btw, if y'all don't much well-nigh this basic concepts thus I too propose y'all become through a comprehensive Java course of teaching similar The Complete Java MasterClass to acquire them inwards a structured way. It's too most up-to-date degree in addition to updated for Java 12.
And, hither is our consummate Java programme to convert hostname to IP address, y'all tin run it either on the ascendancy draw or on your favorite ides similar Eclipse, IntellJIDEA or NetBeans.
Java Program to convert hostname to IP Address
import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; import java.util.Scanner; /** * Java programme to acquire IP address from hostname inwards Java * * @author WINDOWS 8 * */ public class HelloAndroid { public static void main(String args[]) { // Creating Scanner to read input from Console Scanner console = new Scanner(System.in); System.out.println("Please acquire inwards hostname to abide by IP address"); String hostname = console.nextLine(); // Use static method getByName() to acquire InetAddress of host InetAddress inet = null; try { inet = InetAddress.getByName(hostname); } catch (UnknownHostException e) { // TODO Auto-generated grab block e.printStackTrace(); } // Getting IP address from hostname inwards Java String IPAddress = inet.getHostAddress(); System.out.printf("IP address of host %s is %s %n", hostname, IPAddress); } } Output: Please acquire inwards the hostname to abide by the IP address HP The IP address of host HP is 192.168.1.4
That's all well-nigh how to abide by the IP address from hostname inwards Java. This variety of pocket-size programs volition assistance y'all to acquire in addition to empathize JDK API better. As y'all accept learned around of the key classes from java.net bundle inwards this programme similar InetAddress which contains both hostname in addition to IP Address in addition to abstraction to stand upwards for an network address.
Further Reading
The Complete Java MasterClass
Java Fundamentals: Input/Output
Java Network Programming - fourth Edition
TCP/IP in addition to Networking Fundamentals for information technology Pros
Thanks for reading this article thus far. If y'all similar this article thus delight portion alongside your friends in addition to colleagues. If y'all accept whatsoever upshot on agreement this programme or running it from the ascendancy draw or Eclipse, delight driblet a regime annotation in addition to I'll attempt my best to assistance you.
No comments:
Post a Comment