FUNDAMENTOS DE JAVAFUNDAMENTOS DE JAVAFUNDAMENTOS DE JAVAFUNDAMENTOS DE JAVA
CAPÍTULO 8: LECTURA DESDE TECLADOCAPÍTULO 8: LECTURA DESDE TECLADOCAPÍTULO 8: LECTURA DESDE TECLADOCAPÍTULO 8: LECTURA DESDE TECLADO
Ing. Ronald Criollo
LECTURA DE TECLADO
InputStreamReader y BufferedReaderInputStreamReader y BufferedReaderInputStreamReader y BufferedReaderInputStreamReader y BufferedReader
� InputStreamReader convierte bytes a caracteres.� InputStreamReader isr = new InputStreamReader(System.in);
� BufferedReader es capaz de leer hasta un fin de línea.� BufferedReader br = new BufferedReader (isr);
� Para la lectura de líneas es a través del método readLine� String cadena = br.readLine(); //CARACTERES
� int numero = Integer.parseInt (br.readLine()); //NUMERO
LECTURA DE TECLADO
ScannerScannerScannerScanner
� La clase Scanner facilita la tarea de realizar lalectura desde teclado. Ejm:
Scanner sc = new Scanner(System.in);
String cadena = sc.nextLine(); //CARACTERES
int entero = sc.nextInt(); //ENTEROS
SCANNER
� Tiene varios constructores que admiten, además deSystem.in, cosas como secuencias de bytes oficheros.
� Admite Expresiones Regulares como patrones debúsqueda, por lo que podemos leer trozos de líneadirectamente usando los separadores que queramoso buscando expresiones concretas.
SCANNER
ScannerScannerScannerScanner scscscsc ==== newnewnewnew Scanner(SystemScanner(SystemScanner(SystemScanner(System....in)in)in)in);;;; //////// EjmEjmEjmEjm:::: 11111111::::33333333::::44444444
// Usamos como delimitador el dos puntos, o bien cualquier
// espacio/fin de línea (el \\s)
scscscsc....useDelimiter("[useDelimiter("[useDelimiter("[useDelimiter("[::::\\\\\\\\s]")s]")s]")s]");;;;
// Leemos los tres enteros
intintintint aaaa ==== scscscsc....nextInt()nextInt()nextInt()nextInt();;;;
intintintint bbbb ==== scscscsc....nextInt()nextInt()nextInt()nextInt();;;;
intintintint cccc ==== scscscsc....nextInt()nextInt()nextInt()nextInt();;;;
// Obtendremos 11-33-44 de salida.
SystemSystemSystemSystem....outoutoutout....println(aprintln(aprintln(aprintln(a +"+"+"+"----"+"+"+"+ bbbb +"+"+"+"----"+"+"+"+ c)c)c)c);;;;