0

I'm trying to verify if a String s match/is a real number. For that I created this method:

public static boolean Real(String s, int i) {

  boolean resp = false;
  //
  if ( i == s.length() ) {
     resp = true;
  } else if ( s.charAt(i) >= '0' && s.charAt(i) <= '9' ) {
     resp = Real(s, i + 1);
  } else {
     resp = false;
  }
  return resp;
}

public static boolean isReal(String s) {

  return Real(s, 0);
}

But obviously it works only for round numbers. Can anybody give me a tip on how to do this?

P.S: I can only use s.charAt(int) e length() Java functions.

bl4ck code
  • 95
  • 1
  • 1
  • 8

4 Answers4

0

You need to use Regex. The regex to verify that whether a string holds a float number is:

^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
Nami
  • 1,215
  • 11
  • 21
0
public static boolean isReal(String str) {
    boolean real = true;
    boolean sawDot = false;
    char c;
    for(int i = str.length() - 1; 0 <= i && real; i --) {
        c = str.charAt(i);
        if('-' == c || '+' == c) {
            if(0 != i) {
                real = false;
            }
        } else if('.' == c) {
            if(!sawDot)
                sawDot = true;
            else
                real = false;
        } else {
            if('0' > c || '9' < c)
                real = false;
        }
    }
    return real;
}
0

You could try doing something like this. Added recursive solution as well.

public static void main(String[] args) {
    System.out.println(isReal("123.12"));
}

public static boolean isReal(String string) {
    boolean delimiterMatched = false;
    char delimiter = '.';
    for (int i = 0; i < string.length(); i++) {
        char c = string.charAt(i);
        if (!(c >= '0' && c <= '9' || c == delimiter)) {
            // contains not number
            return false;
        }
        if (c == delimiter) {
            // delimiter matched twice
            if (delimiterMatched) {
                return false;
            }
            delimiterMatched = true;
        }
    }
    // if matched delimiter once return true
    return delimiterMatched;
}

Recursive solution

    public static boolean isRealRecursive(String string) {
         return isRealRecursive(string, 0, false);
    }

    private static boolean isRealRecursive(String string, int position, boolean delimiterMatched) {
        char delimiter = '.';
        if (position == string.length()) {
            return delimiterMatched;
        }
        char c = string.charAt(position);
        if (!(c >= '0' && c <= '9' || c == delimiter)) {
           // contains not number
           return false;
        }
        if (c == delimiter) {
            // delimiter matched twice
            if (delimiterMatched) {
               return false;
            }
            delimiterMatched = true;
        } 
        return isRealRecursive(string, position+1, delimiterMatched);
    }
DonatasD
  • 651
  • 6
  • 14
0

Can anybody give me a tip on how to do this?

Starting with your existing recursive matcher for whole numbers, modify it and use it in another method to match the whole numbers in:

  ["+"|"-"]<whole-number>["."[<whole-number>]]

Hint: you will most likely need to change the existing method to return the index of that last character matched rather than just true / false. Think of the best way to encode "no match" in an integer result.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216