Guide to Object-oriented Programming With Java (University at Buffalo Version)
Chapter 6: Java Accepting and Displaying Data
There are certain Java concepts that will be used throughout this guide, including object (constructor, instantiation), class, data attributes, methods, encapsulation, information hiding, Polymorphism, and inheritance.
The Java System.out OutputStream
You can use the System.out OutputStream to display output. Usually System.out writes to the Eclipse console or the command ine window. System.out.print() displays the parameter values without putting a new line after the output. System.out.println() displays the parameter values and then prints a new line after the output. System.out.printf() allows you to provide "type" placeholders for the parameter values and does not print a new line after the output.
System.out.print("But it shows up on one line.\n");
System.out.println("This is another line. ");
System.out.println("This line starts on a new line. ");
System.out.printf("The price for %s is $%.2f\n", "bananas", 1.49);
System.out CodeThis is 2 print statements. But it shows up on one line. This is another line. This line starts on a new line. The price for bananas is $1.49
System.out OutputThe Java java.text.DecimalFormat Library
You can use the java.text.DecimalFormat class in the java.text library to display formatted output. First, you need to add import java.text.DecimalFormat; before your class statement. Then you need to instantiate a DecimalFormat object with the pattern you want to use to display the data. Now you can use the format mathod.
double total = 1234567.8901;
System.out.print("Total: " + myFormat.format(total));
java.text.DecimalFormat CodeTotal: 1,234,567.89
java.text.DecimalFormat OutputThe Java System.err PrintStream
You can use the System.err PrintStream to display errors.
System.err CodeThis is an error!System.err OutputThe Java System.in InputStream
You can use the System.in InputStream to accept input. Usually System.in is connected to a keyboard reader or Scanner object.
try {
inChar = System.in.read();
System.out.print("You entered ");
System.out.println(inChar);
} catch (IOException e){
System.err.println("Error reading from user");
}
System.in CodeEnter a Character: j
You entered: 106System.in OutputUsing a Scanner With the System.in InputStream
You can use the System.in InputStream to accept input. You need to import the java.io.IOException and java.utl.Scanner utility libraries.
Scanner keyboard = new Scanner(System.in); // create an object to request user input
// Accept input for length from the keyboard and check for errors and exceptions
while(true) {
try { // setup for user inputSystem.
System.out.print("Enter the length as a Double: "); // Prompt for user input
length = keyboard.nextDouble(); // store user input as a Double in the variable length
System.out.println("You entered " + length); // tell the user you received their input
// and it is valid
break; // get out of while loop
} catch ( Exception e) { // test to see if the user did not enter a Double
// the type Exception is built in so store it in an object variable e
System.out.println("Sorry, you did not enter a double.");
System.out.println("So, this exception happened: " + e);
keyboard.next(); // go back to the while loop and try again
} // end the try/catch statements
} // end the while loop
Scanner With System.in CodeEnter the length as a Double: j Sorry, you did not enter a double. So, this exception happened: java.util.InputMismatchException Enter the length as a Double:
Scanner With System.in OutputLet's get started with a Input/Output program!


