0

I have a string input by the user that should represent an IP address like: 123.234.12.8 but being a silly person as they can be sometimes what if they enter: 12#.234.1@.8

I've figured out how check for characters and symbols by looping through the string and making temporary chars/ints using:

tempChar = ipString.charAt(i);
tempInt = Character.getNumericValue(tempChar);

I can flag a 'a' or a 'q' but I can't differentiate between the '.' symbol and others such as '#' or '&' because they're all given the int value -1.

Is there an existing method for preventing users entering any symbol except a '.' or can someone point me in the right direction if I'm going about this wrong?

EDIT: This is in Java.

  • Can you just use a regular expression for this? – MadProgrammer Aug 26 '14 at 04:27
  • Are you just trying to validate this data, or remove the bad characters too? Either way, as @MadProgrammer said, regular expressions are great option here. Please clarify your question to get a better answer. – Marc Baumbach Aug 26 '14 at 04:28
  • 1
    For [example](http://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/) or [example](http://stackoverflow.com/questions/5667371/validate-ipv4-address-in-java) – MadProgrammer Aug 26 '14 at 04:28
  • This might be useful [Remove Special Characters in the String in Java](http://stackoverflow.com/questions/21074485/remove-special-characters-in-the-string-in-java) – lxcky Aug 26 '14 at 04:32
  • I'm trying to validate it, if the IP has invalid characters in it I want to ask the user to input a new one. I'm not familiar with regular expressions, on oracle tutorials right now reading through it. – user2926952 Aug 26 '14 at 04:36
  • @MadProgrammer Perfect, I don't quite understand what's happening so I'll have to read up on regular expressions but it looks like that's what I need to learn :) Thanks for pointing me in the right direction. – user2926952 Aug 26 '14 at 04:41
  • Just test the actual characters instead of the value you get from `Character.getNumericValue` and don't call things tempThis and tempThat -- that's a terrible naming convention. – David Conrad Aug 26 '14 at 04:56

3 Answers3

0

Use a regular expression. In this case, you'll want (roughly):

Pattern pattern = new Pattern.compile("(\\d{1,3}\\.){4}");

which you can then use as follows:

boolean isMatch = pattern.matcher(ipString).matches;

This isn't extremely precise in terms of validating an IP address -- better answers are here. But it does answer your question as written: it excludes symbols except for a "." separating digits.

Community
  • 1
  • 1
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
0

You should use regex.

    String ipString = "123.234.12@8";
    System.out.println(ipString.matches("^\\d+(\\.\\d+){3}$"));

matches will return true only if user inputs a sequence of digits and dots.

public boolean matches(String regex) : Tells whether or not this string matches the given regular expression.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
0

I believe this is what you are looking for:

Community
  • 1
  • 1
dcborg
  • 114
  • 5