Foros del Web » Programación para mayores de 30 ;) » Java »

mejora de funcion.

Estas en el tema de mejora de funcion. en el foro de Java en Foros del Web. Hi all, You take a look at this friends, I guess this function it's can make improvements. Código PHP: System . out . println ( ...
  #1 (permalink)  
Antiguo 12/05/2011, 09:32
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 13 años, 11 meses
Puntos: 5
mejora de funcion.

Hi all, You take a look at this friends, I guess this function it's can make improvements.
Código PHP:

System
.out.println(testNum("10.50"));
........
   public static 
int testNum(String numero) {
      
int bol=0;
      
boolean bole=false;
       
int number ;
       
double num ;
       try {
          if(
bole==false)
          {
               
Integer.parseInt(numero);
              
bol=1bole=true;
          }
           
//System.out.println("it's integer");
       
}       
       catch (
NumberFormatException e) {
           
//e.printStackTrace();
       
}
       
finally {
          try {
              if (
bole==false)
              {
                   
Double.parseDouble(numero);
                   
bol=3bole=true;
              }
          }
          
          catch(
NumberFormatException e) {
           
e.printStackTrace();   
          }
          
          
finally {
              return 
bol;
          }
       }
   } 
Hope this helps all of you.

  #2 (permalink)  
Antiguo 12/05/2011, 10:20
Avatar de Heimish2000  
Fecha de Ingreso: enero-2011
Ubicación: Madrid
Mensajes: 844
Antigüedad: 13 años, 2 meses
Puntos: 89
Respuesta: mejora de funcion.

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.    }
  #3 (permalink)  
Antiguo 12/05/2011, 11:10
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 13 años, 11 meses
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

Etiquetas: funcion
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:28.