Home  >  Article  >  Java  >  Java input stream Scanner/BufferedReader usage example

Java input stream Scanner/BufferedReader usage example

高洛峰
高洛峰Original
2017-01-11 14:01:222098browse

1. Use Scanner

When using it, you need to introduce the package import java.util.Scanner; first define the Scanner object

Scanner sc = new Scanner(System.in);
If To input an integer, then int n = sc.nextInt();
String type, then String temp = sc.next();

For example:

import java.util.Scanner;

public class Test {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(scanner.next());
    }
}

2. Use BufferedReader

You need to introduce import java.io.Reader before use;

BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();

For example:

import java.io.*;

public class Test {

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println(br.readLine());
        } catch (IOException e) {

        }
    }
}

For more Java input stream Scanner/BufferedReader usage examples and related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn