Pergunta de entrevista da empresa LinkedIn

Validate if given string is a number.

Respostas da entrevista

Sigiloso

13 de ago. de 2012

Soln is right. Just make sure with interviewer on what he is expecting. if double then use Double.parseDouble(string s) or float.ParseFloat(String s).

1

Sigiloso

5 de nov. de 2012

I think a good solution can be something like this: String pattern = "[0-9,.]+"; return (number.matches(pattern));

1

Sigiloso

8 de jul. de 2011

public static boolean validate(String s) { try { int i = Integer.parseInt(s); } catch(NumberFormatException e) { return false; } return true; }

1

Sigiloso

17 de jun. de 2012

above solution is wrong as it never said the number is an int, it can be flot, decimal, double or int.

Sigiloso

7 de jan. de 2013

if we are going with regex mihir is on the right track make sure you put (\\+|-) before [0-9]+ since - or + should be allowed also to allow decimal numbers follow this with (\\.)?[0-9]+