Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/05/2011, 11:10
abulon81
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 14 años
Puntos: 5
Respuesta: mejora de funcion.

Cita:
Iniciado por Heimish2000 Ver Mensaje
Excuse me for my english level, I know could be better

The first

if(bole==false)

have no sense because in this point bole ALWAYS will be false.

bol will be 0 if it's not a number, 1 if it's a integer and 3 if it's a double, is this what you want?

A better way to do the same with less code line is:

Código JAVA:
Ver original
  1. System.out.println(testNum("10.50"));
  2. ........
  3.    public static int testNum(String numero) {
  4.       int bol=0;  
  5.        int number ;
  6.        double num ;
  7.        try {
  8.             Integer.parseInt(numero);
  9.             bol=1;
  10.        }        
  11.        catch (NumberFormatException e) {
  12.           try {
  13.             Double.parseDouble(numero);
  14.             bol=3;
  15.           }
  16.           catch(NumberFormatException e) {
  17.                 e.printStackTrace();    
  18.           }
  19.        }
  20.        finally {
  21.         return bol;
  22.           }
  23.        }
  24.    }
no worries mate, youre right is less code and the results are the same. Thanks a lot my friend.
Cheers.

Última edición por abulon81; 12/05/2011 a las 11:21