lunes, 14 de marzo de 2022

Java – How to read input from the console - 3 ways

https://mkyong.com/java/how-to-read-input-from-console-java/ 


Java – How to read input from the console

In Java, there are three ways to read input from a console :

  1. System.console (JDK 1.6)
  2. Scanner (JDK 1.5)
  3. BufferedReader + InputStreamReader (Classic)

1. System.console

Since JDK 1.6, the developer starts to switch to the more simple and powerful java.io.Console class.

JavaConsole.java

package com.mkyong.io;

import java.io.Console;

public class JavaConsole {

    public static void main(String[] args) {

        Console console = System.console();

        String input = "";
        while (!"q".equalsIgnoreCase(input)) {

            System.out.print("Enter something (q to quite): ");

            input = console.readLine();
            System.out.println("input : " + input);
        }

        System.out.println("bye bye!");
    }

}

The System.console() will return null in IDE, running the class in console or terminal manually.



~/projects/target/classes$ java com.mkyong.io.JavaConsole

Enter something (q to quite): hello 123
input : hello 123
Enter something (q to quite): hello Java
input : hello Java
Enter something (q to quite): mkyong
input : mkyong
Enter something (q to quite): q
input : q
bye bye!

P.S More Java System.console() examples.

2. Scanner

Before JDK 1.6, this is the Scanner way to read input from the console.

JavaScanner.java

package com.mkyong.io;

import java.util.Scanner;

public class JavaScanner {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String input = "";
        while (!"q".equalsIgnoreCase(input)) {

            System.out.print("Enter something (q to quite): ");

            input = scanner.nextLine();
            System.out.println("input : " + input);
        }

        System.out.println("bye bye!");
    }

}

Enter something (q to quite): hello mkyong
input : hello mkyong
Enter something (q to quite): jdk 1.5
input : jdk 1.5
Enter something (q to quite): exit
input : exit
Enter something (q to quite): q
input : q
bye bye!

P.S More Java Scanner examples examples.

3. BufferedReader + InputStreamReader

In the old days, JDK 1.1, we use BufferedReader + InputStreamReader to read input from the console.

JavaBufferedReaderClassic.java

package com.mkyong.io;

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

public class JavaBufferedReaderClassic {

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            br = new BufferedReader(new InputStreamReader(System.in));

            String input = "";
            while (!"q".equalsIgnoreCase(input)) {

                System.out.print("Enter something (q to quite): ");

                input = br.readLine();
                System.out.println("input : " + input);
            }

            System.out.println("bye bye!");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

JDK 1.7 try-with-resources

JavaBufferedReader.java

package com.mkyong.io;

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

public class JavaBufferedReader {

    public static void main(String[] args) {

        // jdk 1.7
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            String input = "";
            while (!"q".equalsIgnoreCase(input)) {

                System.out.print("Enter something (q to quite): ");

                input = br.readLine();
                System.out.println("input : " + input);
            }

            System.out.println("bye bye!");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

No hay comentarios:

Publicar un comentario