Import Java - Io. Public Class Readcharacter (Public Static Void Main (String Args) Throws Ioexception (System - Out.Print ("Enter Character: ")
Import Java - Io. Public Class Readcharacter (Public Static Void Main (String Args) Throws Ioexception (System - Out.Print ("Enter Character: ")
java
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!!
} }
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)
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."); } }
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(); } }