Q: Splitting an IP Address string to check if valid, and if valid: create array to store each value So in my main client method, I have : 
IPAddress a1 = new IPAddress("153.0012.60.02");

Then in my IPAddress class, I have: 
public class IPAddress {
private int[] parts;

public void reset() {
    parts = new int[4];
}

above is supposed to instantiates the instance variable array parts to an array of size 4 
public static boolean isValidElement(String token) {
    String[] validString = token.split("\\."); 
    if (validString.length != 4)
        return false;
    for (String str: validString ) {
        int i = Integer.parseInt(str); 
        if ((i < 0) || (i > 255)) { 
            return false; 
        } 
    }
        return true; 

}

above is supposed to return true if parameter is the String representation of an integer


*

*between 0 and 255 (including 0 and 255), false otherwise.

*Strings "0", "1", "2" .... "254", "255" are valid.

*Padded Strings (such as "00000000153") are also valid 
public void setParts(String ip) {

//to be completed

}

If the ip address from the String passed is valid,


*

*sets the instance variable parts to store it as 4 integer values.

*For example, if ip = "192.000168.0.0000001", parts should become {192,168,0,1}.


*If the ip address passed is invalid, parts should become {0,0,0,0}


Is 
public void reset();

and 
public static boolean isValidElement(String token)

correct? 
Any help appreciated, thanks

A: your method isValidElement is taking parameter as string but you are passing object type of IPAddress . either you need to change your code according to the object IPAddress  or directly pass the string into the isValidElement method as mention below
    String a1="153.0012.60.02";
    System.out.println(isValidElement(a1));

for storing the ip address take a array in your class 
        static int[] arr =new int[4];

and change your method as mention below
 public static boolean isValidElement(String token) {
 String[] validString = token.split("\\."); 
    if (validString.length != 4)
     return false;
    int j=0;
    for (String str: validString ) {
    int i = Integer.parseInt(str); 
    arr[j]=i;
    j++;
    if ((i < 0) || (i > 255)) { 
        return false; 
    } 
    }
    return true; 

   }


A: Don't do that. Do not re-invent the wheel.
Just do some research, to find how other people are validating regular expressions. For example like this.
Of course, you could do this for the "learning" challenge. But be assured: it is more complicated than it sounds on the surface. So if this is for some sort of product; then just re-use what others have crafted before.
And finally: are you sure that your IP addresses will be IPv4, always? You see, IPv6 is much more complicated; but well, "somehow around the corner"; so you better think now if your code is really good when only supporting v4, but not v6.
