You could help me with the implementation of a java code for data validation, for example, by typing a String "1234"
, or "12.34"
, or "12.34\n"
or "\n"
, or well that does not exceed the number of digits required (I do not know if the forms of income are understood).
That is to say that the entered data goes through different restrictions and force the user to enter what the program asks for, and in the case of being the "\n"
convert it to the character "%code%"
.
I think this is what the programmers call "error trapping" or junk code ... I hope you have understood what I mean, since looking for this topic on the net is not entirely clear, and it would help me codes were a little better.
/* Código de Java: Validación de dato */
public class ValidarAcceso {
public static void main( String[] args ){
final int MIN = -1000;
final int MAX = +1000;
int inicio;
int termina;
double respuesta;
System.out.println("Este programa calcula la suma de los cuadrados de enteros.\n"+
"en un rango entre -1000 y +1000.\n"+
"Ingresa los límites (0 en los dos límites para terminar)\n"+
"Límite bajo: " );
inicio = obten_int();
System.out.println("Limite alto: ");
termina = obten_int();
while( inicio != 0 || termina != 0)
{
if( mal_limites( inicio, termina, MIN, MAX ) )
System.out.println( "Otra vez por favor\n" );
else
{
respuesta = suma_cuadrados( inicio, termina );
System.out.println("La suma de los cuadrados de los enteros ");
System.out.printf("entre %d y %d es %g\n", inicio, termina, respuesta );
}
System.out.println("Entra los límites (0 en los dos "+
" para terminar)\n");
System.out.println( "limite bajo: " );
inicio = obten_int();
System.out.println( "limite alto: " );
termina = obten_int();
}
System.out.println( "Listo.\n" );
}
public static int obten_int()
{
Scanner scan = new Scanner(System.in);
int ingreso;
while( ( ingreso = scan.nextInt() ) != 1 )
{
String cadena = new String( scan.nextLine() );
while( ( scan.nextLine() ) !="\n" )
cadena.charAt(ingreso);
System.out.println(" no es entero.\nFavor entrar un ");
System.out.println( "entero como 25, -178, o 3: ");
}
return ingreso;
}
public static double suma_cuadrados( int a, int b )
{
double total = 0;
int i;
for( i = a; i <= b; i++ )
total +=i * i;
return total;
}
public static boolean mal_limites( int inicio, int termina, int bajo, int alto )
{
boolean no_bueno = false;
if( inicio > termina )
{
System.out.printf("%d no es menor que %d.\n", inicio, termina );
no_bueno = true;
}
if( inicio < bajo || termina < bajo )
{
System.out.printf("Valores deber ser >= %d.\n", bajo );
no_bueno = true;
}
if( inicio > alto || termina > alto )
{
System.out.printf("Valores deber ser <= %d.\n", alto );
no_bueno = true;
}
return no_bueno;
}
}