String corresponding to real number

Asked

Viewed 1,044 times

3

I am trying to check if a String x corresponds to a real number (any one). For this I created the following 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 this method only serves to whole, and not real. Could you suggest me how to do this check?

P.S.: I can only use the functions: s.charAt(int) and length() java.

  • When you say "real" you mean decimal numbers, like 1,23? Or you want something like floating point, which is accepted by the language itself (like 1.23, or even 1.2e3?) and later can be converted into a number? I gave my answer to the simplest case, but it can be adapted if necessary.

  • Yes, numbers like 4.0, 5.1, 1651.9

2 answers

1


I suggest adding a parameter to your function, indicating whether you have already found the comma or not:

public static boolean Real(String s, int i, boolean virgula) {
    ...
    else if ( s.charAt(i) == ',' && !virgula ) {
        resp = Real(s, i + 1, true); // Passa virgula pra true
    } else if ( s.charAt(i) >= '0' && s.charAt(i) <= '9' ) {
        resp = Real(s, i + 1, virgula); // Repete virgula
    } ...
}

public static boolean isReal(String s) {
    return Real(s, 0, false);
}

It would also be interesting to test by - front. Since this only occurs once (at the beginning of the string), it is best to do in the base case (isReal(String)), instead of in the recursive function.

1

You can do so, you validate if it really is a number and if it is, if it is a real number.

public static boolean isReal(String s){
    boolean isNumber = false;
    boolean isReal = false;
    for(int x=0; x<s.length(); x++){
        if((s.charAt(x) >= '0' && s.charAt(x) <= '9') || s.charAt(x) == '.'){ //verifico se é um numero e/ou se contem um ponto '.'
            isNumber = true;
            if(s.charAt(x) == '.' && x>0 && x<s.length()-1){ //aqui eu verifico se o ponto nao esta no começo ou no fim da string.
                if(!isReal) //se o numero ja tiver sido validado como real, ele ja tem um ponto, se tiver outro nao é um numero valido
                    isReal = true;
                else
                    return false;
            }else{
                if(s.charAt(x) == '.') //se for um segundo, ou superior, ponto da string e estiver nas estremidades finaliza.
                    return false;
            }
        }else{
            return false;
        }
    }
    if(isNumber && isReal) //se os dois forem verdade entao é a string é um numero este numero é real.
        return true;
    else
    return false;
}

As you can see I checked if it is a number, checking if each character is between 0 and 9 or if it is a '.', if it is within the parameters is a real number. There can be no more than one point to be a valid number and the points cannot be at the ends of the string.

  • That’s a good answer, +1. Some remarks: 1. "points cannot be at the ends of the string" I believe this depends on the definition, i.e. what AP wants; in the Java language itself, for example, 1. and .1 are valid numbers. 2. Every integer is real, but not every real is integer; a number without any point must return true or false? Also depends on the intention. 3. Your last if.. Else could be replaced by return (isNumber && isReal); (or simply return isNumber, depending on item 2).

  • Thank you for the remark. My idea was that the function return true only if the number is real, I check if it is a number only in case there is a letter, in this case I already return false, if it is a number there then I validate if it is a real. But Thank You Very Much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.