0% found this document useful (0 votes)
70 views7 pages

Java Input Handling and IOException

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views7 pages

Java Input Handling and IOException

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Uso de [Link]() ReadCharacter.

java

// la clase IOException pertenece a [Link]

import [Link].*;

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

char c = (char) [Link](); [Link]("\nYou entered: " + c);

} }

EOF [Link] // la clase IOException pertenece a [Link]

import [Link].*;
// 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 = ""; [Link]("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 = [Link](); while (c != -1) { cadena = cadena + (char)c; c = [Link](); }


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

[Link]("You typed in: [" + [Link]() + "]" ); } }

[Link]() e IOException [Link]

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

Leer lnea por lnea del flujo estndar de entrada [Link]

import [Link].*; class SystemIn { public static void main(String[] args) throws IOException { String line; InputStreamReader s = new InputStreamReader([Link]); BufferedReader stdin = new BufferedReader(s); [Link]("Enter your input: "); line = [Link](); [Link]( "You typed: " + line ); [Link](); [Link](); } }

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

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

You might also like