0

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

9
  • I assume you can't just use the built in class which does this? Commented Sep 10, 2016 at 7:33
  • I suggest you write some test cases which explore the boundaries of what is, and is not valid. Then you will be able to say say for yourself whether it is correct. Commented Sep 10, 2016 at 7:35
  • are you getting correct output or not? Commented Sep 10, 2016 at 7:35
  • @Abhishek if I write System.out.println(isValidElement(a1)); int the client, it says the "The method isValidElement(IPAddress) is undefined for the type IPAddressClient Commented Sep 10, 2016 at 7:39
  • @PeterLawrey if I write System.out.println(isValidElement(a1)); int the client, it says the "The method isValidElement(IPAddress) is undefined for the type IPAddressClient. I dont think I am allowed to use the built in class Commented Sep 10, 2016 at 7:40

2 Answers 2

1

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; 

   }
Sign up to request clarification or add additional context in comments.

2 Comments

This is a static method, where is arr defined?
class in which you are calling isValidElement method
0

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.

1 Comment

Right, but this is for an intro to java assignment, so IPv4 is all thats asked of. Also I've seen the link youve attached, but thats too complicated for what I am learning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.