try catch, bucle

Iniciado por leogtz, 13 Febrero 2010, 00:58 AM

0 Miembros y 1 Visitante están viendo este tema.

leogtz

Hola a todos.

Mi pregunta es cómo hacer un bucle mientras se ingresen datos de otro tipo, ejemplo:

Código (java) [Seleccionar]
import java.io.*;
public class pedo
{
       public static void main(String[] args)
       {  
               InputStreamReader isr = new InputStreamReader(System.in);
               BufferedReader br = new BufferedReader(isr);
               int n_elementos;
               while(?)
               {
                       try
                       {
                               n_elementos = Integer.parseInt(br.readLine());
                       } catch(Exception e)
                       {
                               e.printStackTrace();
                       }
               }
       }
}


Repetir el bucle mientras se teclee algo ajeno a un entero, manejar las excepciones.
Código (perl) [Seleccionar]

(( 1 / 0 )) &> /dev/null || {
echo -e "stderrrrrrrrrrrrrrrrrrr";
}

http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com

egyware

usando una variable de control, si no me equivoco ese parser tira la expcepcion de formato de numero erroneo entonces cuando se capture la excepcion haces un boleano falso
Código (java) [Seleccionar]

boolean seguir=true;
while(seguir){
//..
try{
//..
}catch(Exception e){
seguir = false;
}
}

Algo asi.

leogtz

Creo que no me expliqué bien, va de nuevo:

Lo que quiero es pedir un número, y si hay un error, repetir el bucle.
Código (perl) [Seleccionar]

(( 1 / 0 )) &> /dev/null || {
echo -e "stderrrrrrrrrrrrrrrrrrr";
}

http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com

egyware

entonces me explique mal, entonces usaremos do while

Código (java) [Seleccionar]

boolean seguir = false;
do{
//...
try{
//...
}catch(Exception e){
   seguir = true;
}
}while(seguir);

Explicación el DO-WHILE Hace-Repetir
EL do while realiza primero la accion dentro del bloque y si la condicion es verdadera(en este caso "seguir") se repite el bloque.
Saludos

leogtz

Gracias, luego lo pruebo y te comento.
Código (perl) [Seleccionar]

(( 1 / 0 )) &> /dev/null || {
echo -e "stderrrrrrrrrrrrrrrrrrr";
}

http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com

43H4FH44H45H4CH49H56H45H

Algo asi podria ser:

Código (java) [Seleccionar]
import java.io.*;
public class Main {

    public static void main(String[] args) {
         //System.out.println(Integer.MAX_VALUE);
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try
                {
                    String linea = "";
                    int n_elementos;
                    do
                    {
                        linea = br.readLine();
                        n_elementos = Integer.parseInt(linea);
                         System.out.println("Numero: " + n_elementos + "\n");
                    }while(IsNum(linea));
               
                }
                catch(Exception e)
                {
                    System.out.println("error: " + e.getMessage());
                    System.out.println("No es numero o esta fuera del limite");
                }
    }
    private static boolean IsNum(String cadena)
    {
        for(int i=0;i<cadena.length();i++)
            {
               int ascii =(int)cadena.charAt(i);
               if(ascii <48 ||ascii > 57 ) return false;
            }
        return true;
    }
}

-R IP
:0100
-A 100 
2826:0100 MOV AH,09
2826:0102 MOV DX,109
2826:0105 INT 21
2826:0105 MOV AH,08
2826:0105 INT 21
2826:0107 INT 20
2826:0109 DB 'MI NICK ES CODELIVE.$' 
2826:0127 
-R BX
:0000
-R CX
:20
-N CODELIVE.COM
-W