Ver Mensaje Individual
  #3 (permalink)  
Antiguo 21/06/2010, 03:21
albertito87
 
Fecha de Ingreso: agosto-2009
Mensajes: 38
Antigüedad: 14 años, 8 meses
Puntos: 1
Respuesta: Problema con try y catch

Así lo haría yo:
Código PHP:
public void prueba () {
        
String linea "";
        
int numero 0;
        while (!
linea.equals("fin")) {
            try{
                
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                
linea br.readLine();
                        
numero Integer.parseInt(linea);
                
System.out.println (numero); // Imprime la linea que escribes (es un eco)
                
            
}
            catch(
NumberFormatException e){
                        
linea linea.toLowerCase(); // pasa a minusculas por si escribes "FIN" por ejemplo
                
if (!linea.equals("fin")){
                    
System.out.println ("Ingrese un valor numérico");
                }
            }
            catch (
IOException iOE) {
                
iOE.printStackTrace();
            }
        }
    } 

Última edición por albertito87; 21/06/2010 a las 04:34