ARCHIVO Leer.javaimport java.io.*;
public class Leer
{ public static String dato()
{ String Cadena = "";
try
{ /*InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader Entrada = new BufferedReader(isr);
Cadena = Entrada.readLine();*/
// Otra alternativa para declarar la Entrada de Datos
BufferedReader Entrada = new BufferedReader(new InputStreamReader(System.in));
Cadena = Entrada.readLine( );
}
catch(IOException e)
{ System.err.println("Error : " + e.getMessage());
}
return Cadena;
}
public static short datoShort()
{ try
{ return Short.parseShort(dato());
}
catch(NumberFormatException e)
{ return Short.MIN_VALUE;
}
}
public static int datoInt()
{ try
{ return Integer.parseInt(dato());
}
catch(NumberFormatException e)
{ return Integer.MIN_VALUE;
}
}
public static long datoLong()
{ try
{ return Long.parseLong(dato());
}
catch(NumberFormatException e)
{ return Long.MIN_VALUE;
}
}
public static float datoFloat()
{ try
{ Float f = new Float(dato());
return f.floatValue();
}
catch(NumberFormatException e)
{ return Float.NaN;
}
}
public static double datoDouble()
{ try
{ Double d = new Double(dato());
return d.doubleValue();
}
catch(NumberFormatException e)
{ return Double.NaN;
}
}
}
Te guardas ese archivo Leer.java en la misma carpeta donde tienes a tu otro fichero donde esta tu programa.
Aqui un ejercicio para que comprendas mejor.
Hallar A+B-C+100class JavaAritmetica1
{
public static void main (String mago [])
{
int A, B, C;
System.out.print ("Inserte A: ");
A = Leer.datoInt ();
System.out.print ("Inserte B: ");
B = Leer.datoInt ();
System.out.print ("Inserte C: ");
C = Leer.datoInt ();
System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100));
}
}
Estos tambien te pueden servir:
Ejercicios resueltos [programacion estructurada]Para ver este enlace Registrate o Inicia Sesionhttp://foro.portalhacker.net/index.php/topic,60805.0.html