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?

  • Try testing it... – brso05 Apr 13 '17 at 13:52
  • Please let us know your results. – brso05 Apr 13 '17 at 13:53
  • What makes you think that there is a huge difference? And what makes you think that such experiments matter in reality? – GhostCat Apr 13 '17 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? – Alex Papageorgiou Apr 13 '17 at 13:58
  • Go with `Scanner`... – brso05 Apr 13 '17 at 14:04
  • @AlexPapageorgiou Hehe ... that was my whole point. Whenever a HUMAN is involved, you think that nanoseconds matter? Beyond that: you don't need a human. The point is that system.in reads from stdin. So at least on the linux side, you should be able to **pipe** into the Java process; and then stdin should see that input. Like `cat bigfile | java yourtest` ... just do some research there ... you could find: http://stackoverflow.com/questions/5724646/how-to-pipe-input-to-java-program-with-bash for example! – GhostCat Apr 13 '17 at 14:47
  • Using Scanner is a whole lot more readable than the abomination that is trying to combine both of them (IMO). Even if there's a minor performance difference, I'd never recommend doing that. If I/O performance is critical enough that shaving off a few percent, if it would even be that much, is going to make all the difference (which is rarely, if ever, the case), you should probably drop Scanner altogether, if not Java as a whole, in favour of working on a lower level, but first optimise everything else and also benchmark the simplest version to make sure you're not unnecessarily optimising. – Bernhard Barker Apr 13 '17 at 16:45
  • As I mentioned, this was purely for academic purposes. I just find System.console().readLine() more handy (IMO) as opposed to Scanner due to not needing an import. I am fine with writing a few extra lines to filter input in order to use System.console().readLine() if it is meant for User Input (I understand how scanner would be more viable for reading a million ints from a txt file for example). – Alex Papageorgiou Apr 13 '17 at 20:30

0 Answers0