2

Hello basically I tried to use this code

    for(int character=0; character<roomNo.length(); character++){
        if((Character.isDigit(roomNo.charAt(character)))) {
        }
    }
    return true;

To loop through a String and see if it contains any numbers. I'm trying to create a method that checks whether a String is numeric, if it is the method should return true. So far it doesn't work? Any help would be appreciated :)

2
  • 2
    What do you mean by a "numeric value"? Do you consider -2.3 to be a numeric value? Do you mean a strictly positive integer? Commented Dec 2, 2012 at 21:35
  • Does this answer your question? How to check if a String is numeric in Java Commented Feb 1, 2024 at 15:04

7 Answers 7

9

You can check this using regexp:

roomNo.matches("\\d+");
Sign up to request clarification or add additional context in comments.

1 Comment

@A.R.S. some mysterious processes occur here:)
4

Why not just do roomNo.matches("\\d+")?

\d matches any digit and, consequently, \d+ matches any string of only digits.

2 Comments

Poor bellum was the first to point that out and has less upvotes :p
@Keyser i missed at first one important character:)
1

Since it's a room number, I'm assuming that you're looking for an Integer, so I'd recommend Integer.parseInt().

2 Comments

Who says there aren't more than 2,147,483,647 rooms? :-)
Using parseInt just to validate a string is unwieldy and a validation failure incurs the additional cost of the exception mechanism.
1

The usual form of an explicit loop for this sort of validation is:

for each character in the string
  if not acceptable
    return false
return true

There are at least two alternatives that avoid an explicit loop, a regular expression (already suggested) and attempting conversion to the appropriate type in a try-catch.

For example, if you want an integer, call Integer.parseInt and catch NumberFormatException. If the exception happens, return false. If not, return true. The conversion strategy is especially useful for the more complicated formats, such as double.

Comments

0

Try this:

    for(int character=0; character<roomNo.length(); character++){
    if(!Character.isDigit(roomNo.charAt(character))) {
         return false;
    }
}
return true;

Or as others have said, use regular expressions

6 Comments

The loop here will return true as soon it sees a digit.
I modified it. When i read "To loop through a String and see if it contains any numbers" i thought he wants to know if the string contains any number, not ONLY numeric values
This one worked most easily, does this just mean if it finds a letter it will return false. Basically I'm trying to check a 3 character String on whether its numeric and whether parts of it are within a certain range.
I'm looking to check the String and if it contains even one letter the method has to return false.
Yes, that's what it means. If you want to check parts of the String, you can use the Integer.parseInt() method on some substrings of that string to get the values. It is something like this: Integer.parseInt(myString.substring(0,1)) . This is if you want to check certain parts of that numeric string to be in a certain range. If you want to take the digits separately, just use the charAt() method and remember that digit 0 has the ASCII code 48. If you want to check if it contains a single letter, use the isLetter() method.
|
0

You could so something like

String numbers = "0123456789"
if(numbers.indexOf(roomNo.charAt(character)) >= 0)
...

Comments

0

I would suggest using NumberUtils from Apache Commons

Apache Commons isNumber(String)

Apache Commons isDigits(String)

Personal preference is to use a proven implementation rather than rolling my own.

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.