0% found this document useful (0 votes)
26 views

Import Java - Io. Public Class Readcharacter (Public Static Void Main (String Args) Throws Ioexception (System - Out.Print ("Enter Character: ")

This document contains code examples that demonstrate reading input from the standard input stream in Java. It shows how to read a single character, read multiple characters until end-of-file, read an entire line, and read and sum numbers entered one per line until no more input is given. The code uses System.in.read() to read characters and BufferedReader.readLine() to read lines, and it handles exceptions from reading input.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Import Java - Io. Public Class Readcharacter (Public Static Void Main (String Args) Throws Ioexception (System - Out.Print ("Enter Character: ")

This document contains code examples that demonstrate reading input from the standard input stream in Java. It shows how to read a single character, read multiple characters until end-of-file, read an entire line, and read and sum numbers entered one per line until no more input is given. The code uses System.in.read() to read characters and BufferedReader.readLine() to read lines, and it handles exceptions from reading input.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Uso de System.in.read() ReadCharacter.

java

// la clase IOException pertenece a java.io

import java.io.*;

public class ReadCharacter { public static void main(String[] args) throws IOException { System.out.print("Enter character: ");
// System.in.read() throws an IOException, // which the method calling it -- main --- throws back // we don't worry about this for now!!

char c = (char) System.in.read(); System.out.println("\nYou entered: " + c);

} }

EOF ReadCharacters1.java // la clase IOException pertenece a java.io

import java.io.*;
// this program must be executed under a DOS windows (not netbeans) // as EOF (Ctrl-Z) doesn't work in Netbeans output window

class ReadCharacters1 { public static void main(String args[]) throws IOException { String cadena = ""; System.out.println("Enter characters (Ctrl-Z on new line to finish)!!");
// the Ctrl-Z command will only work in a DOS window, // not in Scite environment (or netBeans)

int c = System.in.read(); while (c != -1) { cadena = cadena + (char)c; c = System.in.read(); }


// the method trim remove the blanks at the end of a string

System.out.println("You typed in: [" + cadena.trim() + "]" ); } }

System.in.read() e IOException ReadCharacters0.java

Gestin de Excepciones ReadCharacters2.java // la clase IOException pertenece a java.io import java.io.*; // en vez de lanzar la excepcin lanzada por System.in.read() // fuera del main (como en ReadCharacter2) // atrapamos el error y imprimimos un mensaje class ReadCharacters2 { public static void main(String args[]) { try { String cadena = ""; System.out.println("Enter characters (Ctrl-Z on new line to finish)!!"); int c = System.in.read(); while (c != -1) { cadena = cadena + (char)c; c = System.in.read(); } System.out.println("You typed in: [" + cadena.trim() + "]" ); } catch (IOException ioe) { System.out.println("Problem reading character from the standard input."); } }

Leer lnea por lnea del flujo estndar de entrada SystemIn.java

import java.io.*; class SystemIn { public static void main(String[] args) throws IOException { String line; InputStreamReader s = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(s); System.out.print("Enter your input: "); line = stdin.readLine(); System.out.println( "You typed: " + line ); s.close(); stdin.close(); } }

Sumar diez nmeros entrados por el flujo estndar SumarDiez.java import java.io.*; class SumarDiez { public static void main(String[] args) throws IOException { String line; InputStreamReader s = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(s); int count = 1; int sum = 0; System.out.println("Please introduce numbers, one per line."); while (count <= 10) { // System.out.println("Enter number " + count + ":"); line = stdin.readLine(); sum = sum + Integer.parseInt(line.trim()); count++; } System.out.println("You typed in 10 numbers. The sum of the numbers is " + sum); s.close(); stdin.close(); } }

Sumar N nmeros entrados por el flujo estndar Sumar.java import java.io.*; class Sumar { public static void main(String[] args) throws IOException { System.out.println("Please introduce numbers, one per line."); String line; InputStreamReader s = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(s); int sum = 0; line = stdin.readLine(); while (line != null) { sum = sum + Integer.parseInt(line.trim()); line = stdin.readLine(); } System.out.println("The sum of the numbers is " + sum); s.close(); stdin.close(); } }

You might also like