Lectura de Datos Desde El Teclado
Lectura de Datos Desde El Teclado
Java ofrece una forma fácil de leer datos desde el teclado haciendo uso
del objeto BufferedReader. Este objeto cuenta con diferentes métodos
que podemos utilizar dependiendo de la operación que se quiera realizar.
- 30 -
FUNDAMENTOS DE PROGRAMACIÓN EN LENGUAJE JAVA
class Letras
{
char letra1;
void leerLetra() throws IOException
{
System.out.println(“Digite un caracter?”);
letra1 = (char)System.in.read();
}
}
- 31 -
FUNDAMENTOS DE PROGRAMACIÓN EN LENGUAJE JAVA
{
String cadena1;
void leerCadena(BufferedReader lector1)throws
IOException
{
System.out.println(“Digite su nombre: “);
cadena1 = lector1.readLine();
}
}
Al igual que con los caracteres alfabéticos, los caracteres numéricos que
se ingresan por teclado también son leídos como si fueran cadenas de
tipo String. Para poderlos almacenar en variables numéricas se hace
necesario que una vez leídos se les haga la conversión en el programa.
- 32 -
FUNDAMENTOS DE PROGRAMACIÓN EN LENGUAJE JAVA
}
}
class Taxis
{
int numInterno;
void registrarTaxi(BufferedReader Lectura)throws
IOException
{
System.out.println(“Registre el número interno
del taxi: “ );
/* Conversión de una cadena leída desde el
teclado en un dato de tipo int */
numInterno = Integer.valueOf(Lectura.readLine()).
intValue();
}
}
- 33 -
FUNDAMENTOS DE PROGRAMACIÓN EN LENGUAJE JAVA
class Buses
{
double impuestoRodamiento;
void leerImpuestoRodamiento(BufferedReader Lectura)
throws IOException
{
System.out.println(“Valor impuesto de
rodamiento:”);
/* Conversión de una cadena leída desde el
teclado en un dato de tipo doublé */
impuestoRodamiento =
Double.valueOf(Lectura.readLine()).doubleValue();
}
}
- 34 -
FUNDAMENTOS DE PROGRAMACIÓN EN LENGUAJE JAVA
import java.io.*;
public class LeeBooleanosTeclado
{
public static void main(String[]args)throws IOException
{
BufferedReader BufferedReader1 = new BufferedReader
(new InputStreamReader(System.in));
Casas Casa1 = new Casas();
Casa1.leerCasas(BufferedReader1);
if(Casa1.garaje==true)
System.out.println(“La casa tiene
garaje!”);
else
System.out.println(“La casa no tiene
garaje!”);
}
}
class Casas
{
boolean garaje;
String auxgaraje;
void leerCasas(BufferedReader Lectura)throws IOException
{
System.out.println(“Garaje? SI/NO”);
auxgaraje = Lectura.readLine();
auxgaraje = auxgaraje.toUpperCase();
if(auxgaraje.equals(“SI”))
garaje = true;
else
garaje = false;
}
}
- 35 -
FUNDAMENTOS DE PROGRAMACIÓN EN LENGUAJE JAVA
import java.io.*;
public class AdministracionEdificio
{
public static void main(String[]args)throws IOException
{
BufferedReader BufferedReader1 = new BufferedReader(new
InputStreamReader(System.in));
Apartamentos Apto1 = new Apartamentos();
Mensualidades Mayo = new Mensualidades();
Apto1.leerDatosApartamento(BufferedReader1);
System.out.println(“CUOTA DE AMINISTRACION”);
System.out.println(“APARTAMENTO: “+Apto1.numero +” TIPO:
“+Apto1.tipo+ “ GARAJE: “+Apto1.auxgaraje +
“ VALOR: “ + Mayo.calcularadministracion(Apto1));
}
}
class Apartamentos
{
double numero;
int tipo;
boolean garaje;
String auxgaraje;
void leerDatosApartamento(BufferedReader entrada) throws
NumberFormatException, IOException
{
System.out.println(“DIGITE NUMERO DEL
APARTAMENTO: “);
System.out.flush();
numero = Double.valueOf(entrada.readLine()).
doubleValue();
System.out.println(“DIGITE EL TIPO DE
APARTAMENTO: (1,2,3,4)“);
System.out.flush();
tipo = Integer.valueOf(entrada.readLine()).
intValue();
System.out.println(“EL APARTAMENTO TIENE
GARAJE? SI/NO “);
System.out.flush();
auxgaraje = entrada.readLine();
auxgaraje = auxgaraje.toUpperCase(); //El método
toUpperCase Convierte una cadena a MAYUSCULAS
if (auxgaraje.equals(“SI”)) //El método equals()
pemite evaluar el contenido de una cadena
- 36 -
FUNDAMENTOS DE PROGRAMACIÓN EN LENGUAJE JAVA
garaje = true;
else
garaje = false;
}
}
class Mensualidades
{
double calcularadministracion(Apartamentos Apartamento1)
{
double valcuota;
if(Apartamento1.tipo == 1)
valcuota = 30000;
else if(Apartamento1.tipo == 2)
valcuota = 40000;
else if(Apartamento1.tipo == 3)
valcuota = 50000;
else
valcuota = 70000;
if(Apartamento1.garaje==false)
return valcuota;
else
return valcuota + 20000;
}
}
- 37 -