Side Navigation

X

Read Console Lines (3 ways)

public class BestOnlineLearning {
    public static void main(String[] args) throws IOException {
        System.out.println("------Using buffer Reader:-------- ");
        System.out.print("Enter a word: ");

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();
        System.out.println(s.toUpperCase());

        System.out.println("------Using Scanner class: --------");
        System.out.print("Enter a word: ");

        Scanner scanner = new Scanner(System.in);
        String sScan = scanner.nextLine();
        System.out.println(sScan.toUpperCase());

        System.out.println("------Using System Console: --------");
        System.out.print("Enter a word: ");

        //this will read pre existed lines only
        String sConsole = System.console().readLine();
        System.out.println(sConsole.toUpperCase());
        System.out.println("done !!!");
    }
}

 

You May Also Like

Comments

Leave a Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>