Java System In Out Error Example

This example will tell you how to use java standard input, output, and error object to read string data from user command line input, write string data to standard output console and display error messages to standard error output. All the standard input or output object is the static variable of java.lang.System class.

1. Java System Input Output Variables Overview.

  1. Used to read string data from standard input such as command line console.
  2. java.util.Scanner: A util class that used to wrap to read data. It provides a lot of convenient methods.
  3. System.out: Used to write string data to standard output.
  4. System.err: Write error messages to standard error.

2. Java Input Output Example.

  1. The below example will prompt the user to input id card number.
  2. If the user input is not a number, it will display an error message to the user in the console and prompt again until the user inputs the correct number value.
  3. Below is the example java source code.
    import java.util.Scanner;
    public class SystemIO {
    public static void main(String[] args) {
        // Scanner is a class which wrap to read user input from console.
        Scanner scanner = new Scanner(;
        // Use System.out to print debug info.
        System.out.println("Please input your id card number.");
        // Read user input.
        String line = scanner.nextLine();
        // If user input is not an integer number.
           // Use System.err to print error info.
           System.err.println("Your input is not an integer, input again.");
           // Read user input again.
           line = scanner.nextLine();
         System.out.println("Your input is an integer number. ");
       /* Check whether user input is an integer or not. */
       private static boolean isInteger(String value)
           booleanret = true;
           }catch(NumberFormatException ex)
               ret = false;
               return ret;
  4. Below is the above example execution output.
    Please input your id card number.
    Your input is not an integer, input again.
    Your input is an integer number.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.