Adsence

martes, 11 de enero de 2011

Leer

Leave a Comment
[caption id="attachment_651" align="aligncenter" width="466"]Leer teclado en Java Leer teclado en Java[/caption]

Esta clase no es mia, la e conseguido en Internet, pero puede ser que alguien la necesite, es una clase que permite leer el teclado desde la consola



[java]

/** Clase Leer util para recibir valores mediante el teclado
** Proporcionada por -Scorpion Black-
** http://vscorpionblack.blogspot.com
*/
import java.io.*;

public class Leer
{
// cadenas
public static String dato()
{
String sdato = "";
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader flujoE = new BufferedReader(isr);

sdato = flujoE.readLine();
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
}
return sdato;
}

// enteros cortos
public static short datoShort()
{
try
{
return Short.parseShort(dato());
}
catch(NumberFormatException e)
{
return Short.MIN_VALUE;
}
}

public static byte datoByte()
{
try
{
return Byte.parseByte(dato());
}
catch(NumberFormatException e)
{
return Byte.MIN_VALUE;
}
}

// enteros
public static int datoInt()
{
try
{
return Integer.parseInt(dato());
}
catch(NumberFormatException e)
{
return Integer.MIN_VALUE;
}
}

// enteros largos
public static long datoLong()
{
try
{
return Long.parseLong(dato());
}
catch(NumberFormatException e)
{
return Long.MIN_VALUE;
}
}

//flotantes
public static float datoFloat()
{
try
{
Float f = new Float(dato());
return f.floatValue();
}
catch(NumberFormatException e)
{
return Float.NaN;
}
}

//doble flotante o flotante largo
public static double datoDouble()
{
try
{
Double d = new Double(dato());
return d.doubleValue();
}
catch(NumberFormatException e)
{
return Double.NaN;
}
}
}

/* Y para usarla solo haces esto:

int num1;
System.out.println("Ingresa el numero");
num1=Leer.datoInt();

String nombre;
System.out.println("Ingresa tu nombre:");
nombre=Leer.dato();

*/

[/java]

0 comentarios :