Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I validate an IP Address string? 2

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
I've been looking through the Java API docs and the closest I can find is: InetAddress.getByAddress(byte[] addr) but that needs a 32-bit number. I want to do the same thing, but pass a String in the format: [0-255].[0-255].[0-255].[0-255] and have it tell me if the address is within a legal range... Does a function like that exist in the Java API (not 3rd party download)?
 
From the javadocs:
getByName

public static InetAddress getByName(String host)
throws UnknownHostException

Determines the IP address of a host, given the host's name.

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

don't visit my homepage:
 
Doh! I was just reading the Method Summary which says:
Determines the IP address of a host, given the host's name.
Maybe they should add a bit more info to the method summaries...
 
One more question. The getByName() function will take IP Addresses or DNS names. Is there any function that will only take IP Addresses?

I'm trying to validate an IP Address that I'm passing to an API that only says it accepts IP Addresses, so I don't want DNS names to get through.

Assuming there is no built-in function that validates only IP Addresses, I've come up with this not so great solution to validate that the string fits a very general description of an IP Address:
Code:
if ( ipAddress.matches( "^.[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}/..[0-9]{1,3}" ) == false )
{
	throw new UnknownHostException( "IP Address is an invalid format!  Must be: [0-255].[0-255].[0-255].[0-255]" );
}
Is this right? Is there a better Regex I can use to narrow it down to only allow: "[0-255].[0-255].[0-255].[0-255]"?
 
A regular expressions might be used, but would allow "333.333.333.333".
To restrict it, you could enhance it, but this is much simpler I guess:
Code:
 public static boolean checkIp (String sip)
	{
		String [] parts = sip.split ("\\.");
		for (String s : parts)
		{
			int i = Integer.parseInt (s);
			if (i < 0 || i > 255)
			{
				return false;
			}
		}
		return true;
	}

don't visit my homepage:
 
Thanks. I would have thought with all the classes & functions already in Java that they'd have one that specifically does this, but your function looks like it should work just fine. I added one additional check to make sure it only has 4 octets though:
Code:
public final static boolean ValidateIPAddress( String  ipAddress )
{
	String[] parts = ipAddress.split( "\\." );

	if ( parts.length != 3 )
	{
		return false;
	}

	for ( String s : parts )
	{
		int i = Integer.parseInt( s );

		if ( (i < 0) || (i > 255) )
		{
			return false;
		}
	}

	return true;
}
BTW, does the final keyword have any effect on a static function? I don't get any complaints from the compiler...
 
Diancecht said:
It cannot be overriden.
So does that mean if I have a base class and derived class both with the same static function defined, and I pass a derived object as a reference to the base class, it will call the base class version of the static function?
 
That means your derived class won't even compile as it can't redefine the same method.

Cheers,
Dian
 
Something like this doesn't give me any errors (in Eclipse at least):
Code:
class Base
{
   public static boolean StaticFunc()
   {
      return false;
   }
}

class Derived extends Base
{
   public static boolean StaticFunc()
   {
      return true;
   }
}
But if I make the base version of StaticFunc() final, then I get an error.
 
That's the point: if you make it final, you can't override it

Cheers,
Dian
 
OK thanks. That's what I was wondering.
In C++, the static and virtual keywords are mutually exclusive, so I wasn't sure how Java handled it...
 
In Java, the closest modifier to the C++ virtual that I can think of is abstract, that means the subclasses must implement the method and yes, that's incompatible with static.

The explanation is that a static method cannot be overriden. I know this must be confusing as your previous example compiles, but you're just hiding the method.

Let's hope this example make thins clearer: Override vs Hide

And from the tutorials: Override Tutorial

Cheers,
Dian
 
Aha! Now it makes sense.
I wonder why they don't give you a warning if you hide a static function? :-(
 
I agree, it's like a kind of dark zone in the JVM specification.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top