0

In Java i take Input using standard Scanner and BufferedReader Classes like:

Scanner sc=new Scanner(System.in);
int a=sc.nextInt();

or

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

int a=Integer.parseInt(br.readLine());

but taking input like this takes a lot of running time. I would like to know the faster way to take input. Thanks in advance

Edit: I have seen top java coders like uwi who take input in a very different way. They kinda create their own reader class. I dont get it that how their program becomes fast in runtime.

Nikhil Pathania
  • 812
  • 6
  • 19
  • 3
    What do you mean by "faster"? If you want the user to give input by hand, it'll be as slow as the user decides. – Kayaman Nov 03 '15 at 18:20
  • I don't know what you mean. By far the slowest part of this will be typing the numbers in, and I don't see how you can avoid that. – Paul Boddington Nov 03 '15 at 18:21
  • Read from a file instead of the console. That way you can read much faster than any human could type. – fabian Nov 03 '15 at 18:21
  • i am talking about runtime and not considering user inputting time of user. Basically i want to optimise my runtime of program in compititve coding, so i thought that there might exist a better way to take input fast. – Nikhil Pathania Nov 03 '15 at 18:29
  • it is a different question as i am asking something different @SeeuD1 – Nikhil Pathania Nov 03 '15 at 18:33

5 Answers5

4

In the article below, the author discusses three different ways for reading input from the user in the console. Each way is fairly easy to use and also has its own advantages and drawbacks.

The below mechanisms are discussed and examples are provided. There is also a pros and cons section as well.

Link: http://www.codejava.net/java-se/file-io/3-ways-for-reading-input-from-the-user-in-the-console

Link: Most Efficient Way of Taking User Input in Java

Options:

Scanner Class->

  Scanner reader = new Scanner(System.in);  // Reading from System.in
  System.out.println("Enter a number: ");
  int n = reader.nextInt();

DataInputStream->

 DataInputStream dis = new DataInputStream(System.in);
 int i = dis.readInt();

BufferedReader ->

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = reader.readLine();
System.out.println("Your name is: " + name);

Console->

  Console console = System.console();
  String s = console.readLine();
  int i = Integer.parseInt(console.readLine());
Perdomoff
  • 938
  • 2
  • 7
  • 26
3

Quote from dejavu: "BufferedReader is a lot faster than Scanner because it buffers the character so you don't have to access the file each time you want to read a char from it.

Scanner are of particular use such as reading primitive data type directly and is also used for regular expressions.

I have used Scanner and BufferedReader both and BufferedReader gives significant fast performance. You can test it yourself too."

from:which to choose buffered reader or scanner from performance point of view

Community
  • 1
  • 1
FoldFence
  • 2,674
  • 4
  • 33
  • 57
3

If you want a fast way to input via stdin (and, if I understand you correctly, you want a fast way to repeatedly feed inputs to your programs), your best bet is to pipe or redirect canned responses e.g.

$ java MyReadingClass < mycannedinput.txt

so you can build your class to take interactive input via stdin, and then use a simple shell redirection to feed in canned input such that you don't have to retype it each time.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

The Fastest way to take userinput in java in particularly Competitive coding is to make own FastReader class by using bufferedReader and StringTokenizer.

This method uses the time advantage of BufferedReader and StringTokenizer and the advantage of user defined methods for less typing and therefore a faster input altogether. This gets accepted with a time of 1.23 s and this method is very much recommended as it is easy to remember and is fast enough to meet the needs of most of the question in competitive coding.

Implementation :

static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

In Main File :

 public static void main(String[] args)
    {
        FastReader s=new FastReader();
        int n = s.nextInt();
        int k = s.nextInt();
        int count = 0;
        while (n-- > 0)
        {
            int x = s.nextInt();
            if (x%k == 0)
               count++;
        }
        System.out.println(count);
    }

Source Link : https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/

Jay Dangar
  • 3,271
  • 1
  • 16
  • 35
0

This is repeated question and an optimized way to read and write data in java can be found at the below link.

https://stackoverflow.com/a/49957378/5076337

These classes are very useful in reading inputs and writing outputs in programming contests.

Paul92
  • 137
  • 2
  • 7