2

I need to test whether the string given as input is a not a whole number or contains at least five English alphabets.I know how to implement it using the loops.But I think it affects the efficiency of my code.Can you please tell me any other efficient way to do this?

For eg:

  11111111 //ERROR(Whole number)
  1111aa11  //ERROR(less than 5 English alphabets)
  aAAAAA11  //TRUE
6
  • what code do you have currently? Commented Nov 29, 2011 at 6:11
  • whole number or contains characters ? what do you want your string to be validated for ? number or chars? Commented Nov 29, 2011 at 6:12
  • both .. I need to check whether the string is a whole number or does it contains below 5 alphabets. Commented Nov 29, 2011 at 6:13
  • How do you get the input? a textfield? Commented Nov 29, 2011 at 6:16
  • 1
    your question says "a whole number OR at least five alphabets", but in your example you say "11111111" is error for being a whole number. could you clarify that? Commented Nov 29, 2011 at 6:28

5 Answers 5

4

This is an O(n) problem... never mind what you do you are going to have to check each char in the string to see if its a number or an alphabet char. You could accidentally turn this into a O(n^2) or a O(mn) problem if you are checking for each char against an array of known alphabet and/or numbers. But if you are just using a hashtable, or some ASCII normalization method, then this should be a O(n) solution, and can't really be made any faster. Post your code if you'd like to receive comments on its implementation efficiency.

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

Comments

1

you could do a regex e.g. something like [a-zA-Z]{5,}|[\\d]+

2 Comments

Yah, but regex would still be a O(n) implementation. Its just doing the dirty work behind the scenes for you. :)
@ElfsЯUs, sure, good point, but like you said, the best you can probably do is O(n) and avoid getting into the trap of doing a O(mn). A regex would be cleaner and easier to maintain compared to nested loops IMHO.
0

As per my understandings about you question,

Are looking for to get the length of the string? Then you can use .length() of the string.

You can also check the number is a whole number using Integer.parseInt(string).

Comments

0

You should try Regular expressions, for example, to test a given string is a number, the following could work:

String t = "222x";
String pattern2 = "(\\d+)";     
System.out.println(java.util.regex.Pattern.matches(pattern2, t));

Which would fail, since the string contains a 'x', remove the x and it will return true. You could try the same for the other case.

Hope this is what u are looking for.

Comments

0

Put it in a boolean method first. If it returns true, it is a number, otherwise, it's valid. See example below:

 public boolean validateInput(String x) {

    try {

        Integer.parseInt(x);

    } catch (NumberFormatException ex) {
        return false;
    }

    return true;
}

Comments

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.