Home > Java > javaTutorial > body text

Java input stream Scanner/BufferedReader usage example

高洛峰
Release: 2017-01-11 14:01:22
Original
2111 people have browsed it

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());
    }
}
Copy after login

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) {

        }
    }
}
Copy after login

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!