Standard Stream

TypeFieldSummary
static PrintStreamerrThe "standard" error output stream.
static InputStreaminThe "standard" input stream.
static PrintStreamoutThe "standard" output stream.

public class Main {

  public static void main(String args[]) {
    System.out.println("Java2s.c o m");

  }
}

The output:


Java2s.c o m

Read input from console window

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String[] args) {
    try {
      // read value from console window
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter a value");
      System.out.println(br.readLine());
    } catch (IOException ioe) {
      System.out.println("IO Error :" + ioe);
      System.exit(0);
    }
  }
}

 

The following code reads integer value from console window.

It reads the value as a string type first then convert it to integer with Integer.parseInt method.

 


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String[] args) {
    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter an integer value");
      String input = br.readLine();

      System.out.println("input:" + input);
      System.out.println("convert to integer:" + Integer.parseInt(input));
    } catch (NumberFormatException ne) {
      System.out.println("Invalid value" + ne);
      System.exit(0);

    } catch (IOException ioe) {
      System.out.println("IO Error :" + ioe);
      System.exit(0);
    }
  }
}

 
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.