Java Cheatsheets
Fast I/O from terminal STDIN (user-defined FastReader class) :
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main {
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;
}
}
public static void main(String[] args) {
FastReader s = new FastReader();
int num = s.nextInt();
String word = s.next();
System.out.println("number: " + num);
System.out.println("word: " + word);
}
}
Fast I/O from imported file, rename "template" to filename of choice
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
/* Read files, CHANGE filenames depending on problem. */
BufferedReader f = new BufferedReader(new FileReader("template.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("template.out")));
/* Add any extra variables (e.g. length, amount) here. */
/*******************************************************/
ArrayList < String > arrList = new ArrayList < String > (); //uses arraylist, can also use array here
StringTokenizer st = new StringTokenizer(f.readLine()); //get first line of input file.
while (true) { // loop using st
while (st.hasMoreTokens()) {
arrList.add(st.nextToken());
}
try {
st = new StringTokenizer(f.readLine());
} catch (Exception e) {
break;
}
}
/* Program begins here */
for (String s: arrList) {
System.out.println("index of " + s + " is " + arrList.indexOf(s));
}
System.out.println("size is " + arrList.size());
/* Close files and exit program */
out.close();
f.close();
System.exit(0);
}
}
ArrayList