Test Solution.java with test cases inside input.txt
java Solution.java < input.txt
Solution.java when there are multiple inputs in a single test case
Solution.java
import java.util.*;import java.io.*;public class Solution { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Write your solution here public static void solve() throws IOException { System.out.println("Write your solution inside the solve() method"); // StringTokenizer st = new StringTokenizer(br.readLine()); // Integer.parseInt(st.nextToken()); } public static void main(String[] args) throws IOException { int t = Integer.parseInt(br.readLine()); while(t-- > 0) { solve(); } }}
Solution.java when there is only a single input in a given test case
Solution.java
import java.util.*;import java.io.*;public class Solution { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Write your solution here public static void solve() throws IOException { System.out.println("Write your solution inside the solve() method"); // StringTokenizer st = new StringTokenizer(br.readLine()); // Integer.parseInt(st.nextToken()); } public static void main(String[] args) throws IOException { solve(); }}
If there are many small segments that need to be printed out one by one, or we need to consolidate many small input string pieces into one string. Consolidate them into one string with StringBuilder. Because string in java is Immutability, concatenating 2 strings require us to create a new string. Example: without StringBuilder Buffer, with StringBuilder Buffer
Use BufferedReader, InputStreamReader and StringTokenizer to read in the input, instead of using Scanner
CPP Code Template
Test Solution.cpp with test cases inside input.txt