Jump to content
  • Advertisement
Sign in to follow this  

Help With Splitting Strings (Java)

This topic is 4061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, My google fu isn't strong today, and I just can't find any helpful information on how to split a string in java, or rather an IP Address. I have been programming in Java for about 5 months now and I happened to miss the day they talked about parsing strings in my class at high school.
if (event == joinG)
	msgDisplay = JOptionPane.showInputDialog(
			"Server IP Address? \n (e.g.", ""
	create = false;
	join = true;

Now what needs to be done is that the string that it out puts needs to be split and put into byte form, something like this. (I am currently networking and the way I test is over Hamachii with my friends)
byte[] addr = { (byte) 5, (byte) 154, (byte) 65, (byte) 219 };

Granted something so simple would be so complicated in my mind. I think I also want to have some sort of IP Address checking to make sure the user wasn't a ditz on a keyboard (e.g. "") Any help or pointing me towards a tutorial that does what I need, please let me know

Share this post

Link to post
Share on other sites

String s = "";

try {
InetAddress addr = InetAddress.getByName( s );

byte[] elements = addr.getAddress();
// elements contains individual values that make up the address
} catch (UnknownHostException e ) {
// invalid

This works also if s contains a DNS name ("www.gamedev.net"), in which case it'll be resolved and checked for validity.

Share this post

Link to post
Share on other sites
Very interesting Antheus and very helpful

Wow that was a ton of help and simplified a ton of code to just few lines. Thank you Antheus you have been rated up (^_^)

[Edited by - WarmWaffles on August 13, 2007 12:50:57 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!