Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to Object-oriented Programming With Java (Buffalo State University 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("This is 2 print statements. ");
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);


Figure 6-1: Java System.out Code
This 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

Figure 6-2: Java System.out Output

The 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.

DecimalFormat myFormat = new DecimalFormat("#,###.00");
double total = 1234567.8901;
System.out.print("Total: " + myFormat.format(total));

Figure 6-3: Java java.text.DecimalFormat Code
Total: 1,234,567.89

Figure 6-4: Java java.text.DecimalFormat Output

The Java System.err PrintStream

You can use the System.err PrintStream to display errors.

System.err.print("This is an error!\n");

Figure 6-5: Java System.err Code
This is an error!

Figure 6-6: Java System.err Output

The 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.

System.out.print("Enter a Character: ");
try {
  inChar = System.in.read();
  System.out.print("You entered ");
  System.out.println(inChar);
} catch (IOException e){
  System.err.println("Error reading from user");
}

Figure 6-7: Java System.in Code
Enter a Character: j
You entered: 106

Figure 6-8: Java System.in Output

Using 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.

double length; // create a variable to hold user input
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

Figure 6-9: Using a Scanner With System.in Code
Enter 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: 

Figure 6-10: Using a Scanner With System.in Output

Let's get started with a Input/Output program!

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland