|
Console Input and Output in Java
An Introduction |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
main() is calledSystem.in
System.out
System.err
System.out,
System.err, and/or System.in?PrintStream ObjectPrintStream Object (cont.)String literalsboolean
char
String
PrintStream Object (cont.)
printf()
import java.io.PrintStream;
/**
* An example that illustrates the use of the printf()
* method in the PrintStream class
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class PrintfExample
{
/**
* The entry point of the application
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
int age, weight;
double monday, tuesday, wednesday;
age = 27;
System.out.printf("%d\n", age);
System.out.printf("%10d\n", age);
weight = 155;
System.out.printf("Prof. B. is %d years old\n", age);
System.out.printf("Age: %d\tWeight: %d\n", age, weight);
monday = 35.8;
tuesday = 0.0;
wednesday = -10.2;
System.out.println("\nWith No Flags, Width or Precision");
System.out.printf("%f\n", monday);
System.out.printf("%f\n", tuesday);
System.out.printf("%f\n", wednesday);
System.out.println("\nWith No Flags");
System.out.printf("%5.1f\n", monday);
System.out.printf("%5.1f\n", tuesday);
System.out.printf("%5.1f\n", wednesday);
System.out.println("\nWith + Flags");
System.out.printf("%+5.1f\n", monday);
System.out.printf("%+5.1f\n", tuesday);
System.out.printf("%+5.1f\n", wednesday);
}
}
InputStream ObjectScanner ObjectScanner Object (cont.)
Scanner
import java.util.Scanner;
/**
* An example that illustrates the use of the Scanner class
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class ScannerExample
{
/**
* The entry point of the application
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
int age, weight;
double salary;
Scanner keyboard;
String line, name;
String[] parts;
keyboard = new Scanner(System.in);
System.out.print("Name: ");
name = keyboard.nextLine();
System.out.print("Salary: ");
salary = keyboard.nextDouble();
keyboard.nextLine(); // Consume the end-of-line character
System.out.print("Phone number (###-###-####): ");
line = keyboard.nextLine();
System.out.print("Age Weight (# #): ");
age = keyboard.nextInt();
weight = keyboard.nextInt();
keyboard.close();
}
}
Scanner (cont.)
Scanner Objectsimport java.util.Scanner;
/**
* An example that illustrates an inconvenience assoicated with
* the behavior of Scanner objects.
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class ScannerInconvenienceExample
{
/**
* The entry point of the application.
*
* @param args The command-line arguments (which are ignored)
*/
public static void main(String[] args)
{
int age;
Scanner keyboard;
String name;
keyboard = new Scanner(System.in);
// If you run this program you won't be able to enter your
// name because the return you enter after your age will
// be considered the "next line"
System.out.print("Enter your age: ");
age = keyboard.nextInt();
System.out.print("Enter your name: ");
name = keyboard.nextLine();
keyboard.close();
}
}
Scanner Object (cont.)nextLine() immediately after
invoking one of these methodsConsole object can be retrieved using
the System.console() method and
it has both readLine() and printf()
methodsconsole() method returns null
in IDEs