public class ReadCharacter { public static void main(String ...

mongooseriverSoftware and s/w Development

Jun 7, 2012 (5 years and 5 months ago)

347 views



// 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);


}

}
ReadCharacter.java
Uso de System.in.read()


// 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() + "]" );

}
}
ReadCharacters1.java
EOF


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


// la clase IOException pertenece a java.io
import java.io.*;
// en vez de lanzar la excepción 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.");

}

}
ReadCharacters2.java
Gestión de Excepciones


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();

}

}
SystemIn.java
Leer línea por línea del flujo estándar de entrada


SumarDiez.java
Sumar diez números entrados por el flujo estándar
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.java
Sumar N números entrados por el flujo estándar
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();

}
}