DNNJFM's blog

By DNNJFM, history, 4 years ago, In English,

Abstract

In the following I will describe how to read the input in Java. We will examine the Scanner class and then write our own class for faster input reading.

Using the Scanner class

We can read the input using the Scanner class:

import java.util.*;

public class Main{ public static void main(String[] args) { // Use the Scanner class Scanner sc = new Scanner(System.in);
/* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String */ } } Using the BufferedReader class

However, the Scanner class is slow and may cause a "timelimit exceeded". We can read the input faster with the BufferedReader class. The class "MyScanner" below uses a BufferedReader. The same method names as in the Scanner class have been chosen, e.g. to read the input as an integer we still can use nextInt().

import java.io.*; import java.util.*;

public class Main{ public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out));

// Start writing your solution here. -------------------------------------

  /*
  int n      = sc.nextInt();        // read input as integer
  long k     = sc.nextLong();       // read input as long
  double d   = sc.nextDouble();     // read input as double
  String str = sc.next();           // read input as String
  String s   = sc.nextLine();       // read whole line as String

  int result = 3*n;
  out.println(result);                    // print via PrintWriter
  */

  // Stop writing your solution here. -------------------------------------
  out.close();

}

//-----------PrintWriter for faster output--------------------------------- public static PrintWriter out;

//-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st;

public MyScanner() {
     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;
  }

} //-------------------------------------------------------- } References

ReadLine in Java

If you know of a simpler/better method for input reading in Java, please post it below.

I added PrintWriter for a faster output.

Thanks for reading and comments.

 
 
 
 
  • Vote: I like it
  • +9
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks for your post but I have some notes:

  • I think using throws IOException is easier than try catch. What is your opinion?
  • Storing the output in a StringBuilder and printing it at the end is a little bit faster than using PrintWriter.
  • Did you try to write hasNext()?
»
13 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

if I want to use the output function to out into file what will be the final form of the function like in this problem

upate Finally I found how to use fast input and output with file or with standard

»
6 months ago, # |
  Vote: I like it -9 Vote: I do not like it

This is great but you can also visit here for better understanding.