Nhập xuất màn hình console trong java

Đối với những bạn mới bắt đầu học Java cơ bản, chắc hẳn sẽ sử dụng nhập xuất trong màn hình console rất nhiều để giải các bài tập. Trong bài viết này chúng ta sẽ cần nhau tìm hiểu về nhập xuất trong màn hình console. 

Đọc dữ liệu từ màn hình console

Trong java chúng ta có nhiều cách khác nhau để đọc dữ từ màn hình console, chúng ta sẽ đi qua từng cách ở phần sau.

Sử dụng Buffered Reader Class

Đây là một phương thức đọc dữ liệu từ màn hình console cổ điển được java giới thiệu trong bản JDK1.0. Để sử dụng cách này chúng ta cần khởi tạo một BuffedReader instance bao ngoài  InputStreamReader và System.in.


Đọc chuỗi từ màn hình console

Sử dụng readLine() để đọc một chuỗi từ màn hình console

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Test
{
    public static void main(String[] args) throws IOException
    {
        //Enter data using BufferReader
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Input your name: ");
        String name = reader.readLine();
        System.out.println("Your name is: " + name);
    }
}

Output: 

Input your name: hai
Your name is: hai

Đọc số trong màn hình console

Để đọc số trong màn hình console chúng ta sử dụng readLine() kết hợp với Double.parse() hoặc Integer.parse() để đọc kiểu dữ liệu tương ứng.

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

class Test
{
    public static void main(String[] args) throws IOException
    {
        //Enter data using BufferReader
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Input your age: ");
        int age = Integer.parseInt(reader.readLine());
        System.out.println("your age: " + age);


        System.out.print("Input your height: ");
        double height = Double.parseDouble(reader.readLine());
        System.out.println("your height: " + height);
        
    }
}

Output: Input your age: 12
your age: 12
Input your height: 121.321
your height: 121.321

Sử dụng Scanner class

Đây có lẽ là cách mà mọi người thường hay dùng nhất. Scanner cung cấp nhiều chức năng hơn và chúng có thể phân loại từng kiểu dữ liệu mà người dùng nhập vào như kiểu số nguyên, số thực, chuỗi etc.

Để đọc một chuỗi trong Scanner chúng ta sử dụng method readLine().

import java.io.IOException;
import java.util.Scanner;

class Test
{
    public static void main(String[] args) throws IOException
    {
       
        Scanner scanner = new Scanner(System.in);

        System.out.print("Input your name: ");
        String name = scanner.nextLine();
        System.out.println("your name: " + name);
    }
}

Output: 

Input your name: hai
Your name is: haiĐ

Để nhập xuất kiểu số, chúng ta sử dụng nextInt() để đọc số Integer, nextDouble() để đọc số Double, nextFloat() để đọc số float etc.

import java.util.Scanner;

class Test {
    public static void main(String[] args) {
        //Enter data using BufferReader
        Scanner scanner = new Scanner(System.in);

        System.out.print("Input your age: ");
        int name = scanner.nextInt();
        System.out.println("your name: " + name);


        System.out.print("Input your height: ");
        double height = scanner.nextDouble();
        System.out.println("your height: " + height);

    }
}

Output:

Input your age: 12
your name: 12
Input your height: 21.3123
your height: 21.3123

Sử dụng console class

Sử dụng Console class cũng là một trong những cách thường dùng, đặc biệt nó có thể dùng để đọc password thứ mà không hiện thị lên màn hình console mà chỉ đọc ngầm bên dưới.

import java.io.Console;

class ReadPassword {
    public static void main(String args[]) {

        Console con = System.console();

        // Checking If there is no console available, then exit.   
        if (con == null) {
            System.out.print("No console available");
            return;
        }
        System.out.println("Enter the password: ");
        char[] ch = con.readPassword();

        String pass = String.valueOf(ch);
        System.out.println("Password is: " + pass);
    }
}

Output:

Enter the password: @myPassword
Password is: @myPassword

Note: Nếu output của bạn là No console available thì chắc hẳn console của bạn đang không phải là console chính gốc. Bạn cần mở terminal từ hệ điều hành và run code hiện tại của bạn. Chi tiết hơn đọc ở đây nhé.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x