0

I want to know if a string is a double or not

for example

   value1 = "236.685"   
   value2 = "it is 3"  

I want a code which will return

value1 is a double
value2 is not a double

Community
  • 1
  • 1
Ralph
  • 9
  • 1
  • 4
  • 2
    [Double.parseDouble()](https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble(java.lang.String)) – achAmháin Jul 05 '18 at 14:35
  • 3
    What have you tried so far? – Tobb Jul 05 '18 at 14:35
  • 1
    Java accepts quite a lot of syntaxes for doubles (and rejects some others too.) I'd say first you should decide what are the syntaxes you want to accept as doubles. Then define a regex for it. – kumesana Jul 05 '18 at 14:36
  • We need to see your code, but another idea for you is to write a for-each that looks at each character and if it finds a "." will return a double. This would be a fun exercise, try it and post your code. – MGT Jul 05 '18 at 14:43

3 Answers3

2

You can check that by trying to parse the string with Double.parseDouble() and catching a NumberFormatException. If it doesn't throw this exception, it's a valid double. This code:

private static boolean isDouble(String string)
{
    try
    {
        Double.parseDouble(string);
    }
    catch (NumberFormatException e)
    {
        return false;
    }
    return true;
}

public static void main(String[] args)
{
    String maybeDouble0 = "123.4";
    String maybeDouble1 = "not a double 345";
    String maybeDouble2 = "45";
    String maybeDouble3 = "321.";
    String maybeDouble4 = ".753";
    String maybeDouble5 = "just text";
    System.out.println(maybeDouble0 + ": " + isDouble(maybeDouble0));
    System.out.println(maybeDouble1 + ": " + isDouble(maybeDouble1));
    System.out.println(maybeDouble2 + ": " + isDouble(maybeDouble2));
    System.out.println(maybeDouble3 + ": " + isDouble(maybeDouble3));
    System.out.println(maybeDouble4 + ": " + isDouble(maybeDouble4));
    System.out.println(maybeDouble5 + ": " + isDouble(maybeDouble5));
}

will result in this output:

123.4: true
not a double 345: false
45: true
321.: true
.753: true
just text: false

I prefer this method over a RegEx pattern because it's asking Java directly, instead of assuming to know Java's internal double representations.

Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
1

Use a try catch statement for Double.parseDouble()

try{
    double num = Double.parseDouble(value);
    System.out.println("is a double");
catch (Exception e){
    System.out.println("not a double");
}
achAmháin
  • 4,176
  • 4
  • 17
  • 40
Jiahao Zhao
  • 101
  • 8
  • Jiahao thanks for your answer , but i am usingt this code on a mongo_Java API so i don't have any opportunity to print it – Ralph Jul 05 '18 at 14:42
  • 1
    It's not really necessary to print it. Just making it clear how to tell whichever one is a double or not. – Jiahao Zhao Jul 05 '18 at 14:44
0

You can use a combination of parseInt and parseDouble

The reason you need both is because parseDouble will still parse ints. You can probably combine them, I just wrote two separate functions, one checks ints and the other checks double.

If isInt is false and isDouble is true then it is a double

If both are true, then it is an int(i think that is not a double from your example). If both are false then its not a number.

The last case is never possible.

   public static boolean isInt( String str ){ 
       try{
           Integer.parseInt( str );
           System.out.println("true");
           return true;
       }catch( Exception e ){
           System.out.println("false");
           return false;
      }  
  }


    public static boolean isDouble( String str ){
      try{
          Double.parseDouble( str );
          System.out.println("true");
         return true;
      }catch( Exception e ){
          System.out.println("false");
          return false;
      }
    }
Cycl10ps
  • 49
  • 6
  • Yea, I'm not sure what you're asking but from what I read in the question. The string has to be a double but not an integer. If I'm wrong, it you can just use the isDouble on its own. Either way I assume it would be helpful – Cycl10ps Jul 05 '18 at 14:56