0

I would like to know if using the Scanner class in conjuction with the System.console.readLine() method results in faster input read. To give you an example, I have created the following program using both of the above, only the Scanner class and only the System.console.readLine() method:

Scanner & System.console.readLine():

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.console().readLine());
        while (!in.hasNextInt())
            in = new Scanner(System.console().readLine());
        System.out.println("This is an int: " + in.nextInt());
        System.out.println("This is a single char: " + System.console().readLine());
        System.out.println("This is a whole String input: " + System.console().readLine());
        while (!in.hasNextInt())
            in = new Scanner(System.console().readLine());
        System.out.println("This is again an int: " + in.nextInt());
    }
}

Scanner:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (!in.hasNextInt())
            in.next();
        System.out.println("This is an int: " + in.nextInt());
        System.out.println("This is a single char: " + in.nextLine().charAt(0));
        System.out.println("This is a whole String input: " + in.nextLine()); // copy-pasting the "\n" char will break input.
        while (!in.hasNextInt())
            in.next();
        System.out.println("This is again an int: " + in.nextInt());
    }
}

Console:

public class Test {
    public static void main(String[] args) {
        int input;
        while (true) {
            try {
                input = Integer.parseInt(System.console().readLine());
                break;
            } catch (IllegalNumberFormat e) {}
        }
        System.out.println("This is an int: " + input);
        System.out.println("This is a single char: " + System.console().readLine().charAt(0));
        System.out.println("This is a whole String input: " + System.console().readLine());
        while (true) {
            try {
                input = Integer.parseInt(System.console().readLine());
                break;
            } catch (IllegalNumberFormat e) {}
        }
        System.out.println("This is again an int: " + input);
    }
}

Which of the above would be fastest for equal large amounts of data of each type?

8
  • Try testing it... Commented Apr 13, 2017 at 13:52
  • Please let us know your results. Commented Apr 13, 2017 at 13:53
  • What makes you think that there is a huge difference? And what makes you think that such experiments matter in reality? Commented Apr 13, 2017 at 13:54
  • This is purely for academic purposes. How would I go about testing this since it relies on user input and I cannot script it to be the same time-wise. (I know of the use of System.nanoTime()). I want to know because the university solely focuses on the Scanner class, a more complex object to properly implement compared to System.console().readLine() (at least as I see it). Which is most commonly used? Commented Apr 13, 2017 at 13:58
  • Go with Scanner... Commented Apr 13, 2017 at 14:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.