How can I check if an input is a String of alphabet characters..not int double etc etc...

Recommended Answers

All 16 Replies

For each char c in [charAt(0)..charAt(s.length()-1)] check that it's in the range [a-z] or [A-Z]. Or, if you want a more permissive test, check that it's not in the range [0-9] - that will allow everything but the digits.

For each char c in [charAt(0)..charAt(s.length()-1)] check that it's in the range [a-z] or [A-Z]. Or, if you want a more permissive test, check that it's not in the range [0-9] - that will allow everything but the digits.

yes, u shd do that. btw, you would probably need to use Integer.parseInt somewhere in order to do the comparisons..

btw, you would probably need to use Integer.parseInt somewhere in order to do the comparisons..

No.

I would say to simply do

if (!string.trim().matches("^[+-]?[0-9\\.]+$")) {
    System.out.println("is not a number");
}

The only problem is that that misses things like 001.002.003 (i.e. strings with numbers and multiple dots). The other question is, though, "is 234,567,890.21 a number in your mind"? If so, it just got infinitely more complicated, and even more than that because that number in European notations is "234.567.891,21".

No.

I would say to simply do

if (!string.trim().matches("^[+-]?[0-9\\.]+$")) {
    System.out.println("is not a number");
}

The only problem is that that misses things like 001.002.003 (i.e. strings with numbers and multiple dots). The other question is, though, "is 234,567,890.21 a number in your mind"? If so, it just got infinitely more complicated, and even more than that because that number in European notations is "234.567.891,21".

im curious, is there any reason Integer.parseInt would be a bad approach? you could certainly use that to to solve this problem and the above problem you mentioned. plus its not too complicated an approach. im just curious?

Integer.parseInt will throw an exception that you will have to catch - not a good practice.

commented: Yep. +1

Integer.parseInt will throw an exception that you will have to catch - not a good practice.

thanks apines. yeah that makes sense. learnt something new today!

thanks apines. yeah that makes sense. learnt something new today!

Happy to help ;)

yes, u shd do that. btw, you would probably need to use Integer.parseInt somewhere in order to do the comparisons..

No, that wouldn't be necessary. chars vsn be treated as ints in a lot of ways:

char c = 69;
if (c > 'A' && c < 'Z') char d = 'a'+ (c - 'A'); // a goofy way to downcase

and so forth. So no need to do any conversion, if you're looking at chars directly, unless you want to get the int value. Then you'd just cast to int.


Masidjade's regex will catch a string that can't be matched as a string of digits with a following decimal place, if I'm reading that correctly. I don't know if that's what's looked for, but a simpler regex for the original question would be s.matches("[a-zA-Z]+") - is this all alphabetical characters?
(hey Masijade - what's with putting an optional character at the front of a negated regex? :) )

No, that regex will match all of the following strings

123
123.45
+123
+123.45
-123
-123.45

unfortunately, as already mentioned, it will also match the following strings

1.2.3
+1.2.3
-1.2.3

Also, that original ^ means beginning of String and the $ means end of string to ensure that you are matching the entire string.

Also, the [a-zA-Z] won't work (and \\w would be better, as that is [a-zA-Z_]) as "String" also includes !, $, %, &, and anything else that is not a number. That is why it is better to match a number (and negate that) than it is to try and match all possible non-numbers.

im curious, is there any reason Integer.parseInt would be a bad approach? you could certainly use that to to solve this problem and the above problem you mentioned. plus its not too complicated an approach. im just curious?

There is overhead to doing that parse. Also, the objective is to match the entire string. So, either you do parseInt on every character (which will then fail on positive, negative, and decimal point signs) or you do parseInt on the entire number, and parseLong on the entire number, and parseFloat on the entire number, and parseDouble on the entire number (although a single parseDouble should be enough). As a matter of fact, I would use my regex presented above, and then, to check for the few Strings that might get through, call parseDouble when the regex indicates a number.

Once again, though, all of this gets complicated if separators are allowed (i.e. the comma after every third position) and even more so if it should be "international" capable.

Also, that origianl ^ means beginning of String and the $ means end of string to ensure that you are matching the entire string.

Gotcha. My bad. I really shouldn't try to read regex before coffee.
I guess I'm not sure what the original poster is looking for precisely - I read it as "alpha characters only", but looking again I guess it could be "isn't mappable to an int" (or a double, or whatever you want to check for)

I guess I'm not sure what the original poster is looking for precisely - I read it as "alpha characters only", but looking again I guess it could be "isn't mappable to an int" (or a double, or whatever you want to check for)

I read the post as to identify any string that is not a number (i.e. the phrase "not int double etc etc" from the OP).

How can I check if an input is a String of alphabet characters

and I was looking at that...

Well, goes to show: get the requirements before you write the code.

That could be, too. We won't know unless the OP bothers to respond.

before that, ive gotta thank yall for clearing up my confusion regarding Integer.parseInt!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.