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

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
Alonzo Robbe
  • 465
  • 1
  • 8
  • 23
  • I assume you can't just use the built in class which does this? – Peter Lawrey Sep 10 '16 at 07: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. – Peter Lawrey Sep 10 '16 at 07:35
  • are you getting correct output or not? – Abhishek Sep 10 '16 at 07: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 – Alonzo Robbe Sep 10 '16 at 07: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 – Alonzo Robbe Sep 10 '16 at 07:40
  • @RobBor You mean you are not allowed to pass IPAddress as an argument, but you can use IPAddress in that method. Never the less what you have is pretty simple, I would add unit tests to show you meet all the requirements in the assignment. – Peter Lawrey Sep 10 '16 at 07:42
  • @PeterLawrey Alright, I'll get to writing test cases. Does it look right though? – Alonzo Robbe Sep 10 '16 at 07:44
  • @RobBor broadly, yes for String with numbers. I would have the constructor parse the String and set the parts if correct rather than use a helper method. The main thing missing is a unit test which demonstrate each of the requirements is met. Do the tests and you won't have to ask us. – Peter Lawrey Sep 10 '16 at 07:59
  • This is where a number of JUnit tests will be able to help you. I can already tell, though, that the IP address "this.is.not.valid" will not do what I think you expect. I'll leave it as an exercise to the questioner to determine why this is. – Joe C Sep 10 '16 at 07:47

2 Answers2

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; 

   }
Abhishek
  • 379
  • 1
  • 8
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.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    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 – Alonzo Robbe Sep 10 '16 at 07:50