How can I check if an input is a String of alphabet characters..not int double etc etc...
coding101
0
Junior Poster in Training
Recommended Answers
Jump to PostFor 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.
Jump to Postbtw, 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 …
Jump to PostInteger.parseInt will throw an exception that you will have to catch - not a good practice.
Jump to Postthanks apines. yeah that makes sense. learnt something new today!
Happy to help ;)
All 16 Replies
jon.kiparsky
326
Posting Virtuoso
martin5211
37
Posting Whiz in Training
Dhruv Gairola
-3
Junior Poster in Training
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Dhruv Gairola
-3
Junior Poster in Training
apines
116
Practically a Master Poster
Featured Poster
jon.kiparsky
commented:
Yep.
+1
Dhruv Gairola
-3
Junior Poster in Training
apines
116
Practically a Master Poster
Featured Poster
jon.kiparsky
326
Posting Virtuoso
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
jon.kiparsky
326
Posting Virtuoso
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
jon.kiparsky
326
Posting Virtuoso
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Dhruv Gairola
-3
Junior Poster in Training
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.