Recuperare una stringa e trasformarla in un numero tipo INT

 

 

Ho un oggetto iXField (di tipo XField)

Con il metodo getValue_String() recupero il campo valore, che è una stringa.

Se il valore recuperato non è blank, trasformo poi questa stringa in numerico richiamando il metodo:

Integer.parseInt( iXField.getValue_String())


 

Imposto nella variabile locale Num_Conv il risultato della conversione, ed eseguo i test desiderati.

Il metodo parseInt della classe Integer può sollevare un’eccezione di tipo NumberFormatException che gestisco tramite il try – catch.

 

publicstatic String YGLS2_BU_DRSCFN(XField iXField, Logger logger) {

String sDRSCFN_MessageError=“”;

try{

if (!iXField.getValue_String().equals(” “)) {

intNum_Conv=Integer.parseInt( iXField.getValue_String());

if(( Num_Conv < 0) || (Num_Conv > 99)){

sDRSCFN_MessageError=“Value is not in range”;

}

}

returnsDRSCFN_MessageError;

} catch(NumberFormatException e ) {

return“Value is not a string”;

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *