Java Input Types — Complete Reference TCS NQT / Competitive Coding
Java Input Types
Complete Reference Guide
Scanner • BufferedReader • ArrayList • 2D Arrays • All Patterns
TCS NQT / HackerRank / Competitive Coding Ready
Contents
1 Overview: Scanner vs BufferedReader 2
2 Basic Single Value Inputs 2
2.1 Single Integer Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Single String Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Single Double / Float Input . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Single Character Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Array Input Formats 4
3.1 Space-Separated Integer Array . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Comma-Separated Integer Array . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Bracket Format Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.4 Space-Separated String (Words) Array . . . . . . . . . . . . . . . . . . . . . 7
4 Number Input Patterns 7
4.1 First Line = Size, Second Line = Array (MOST COMMON in TCS) . . . . 8
4.2 Multiple Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3 Mixed Input (int + String on separate lines) . . . . . . . . . . . . . . . . . . 10
4.4 Array Input Without Size (Read Until EOF) . . . . . . . . . . . . . . . . . . 11
5 String Input Patterns 12
5.1 Parsing Array from String Using split() . . . . . . . . . . . . . . . . . . . 12
5.2 Extracting Numbers from Messy String (Most Powerful Pattern) . . . . . . . 13
5.3 Extracting Digits from Continuous String . . . . . . . . . . . . . . . . . . . 15
6 ArrayList Input 16
6.1 Using ArrayList Instead of Array . . . . . . . . . . . . . . . . . . . . . . . . 16
6.2 ArrayList of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7 2D Array Input 17
7.1 Method 1: Flat Comma-Separated Input (fill row-wise) . . . . . . . . . . . . 17
7.2 Method 2: Row-by-Row Matrix Input (standard format) . . . . . . . . . . . 18
8 Advanced TCS Input Patterns 19
8.1 First or Last Element is Array Size . . . . . . . . . . . . . . . . . . . . . . . 19
8.2 Check Equal Average (Extract Digits from String) . . . . . . . . . . . . . . . 21
9 Quick Reference Cheat Sheet 23
1
Java Input Types — Complete Reference TCS NQT / Competitive Coding
1. Overview: Scanner vs BufferedReader
Java offers multiple ways to read input. The two most used in competitive coding are Scanner
and BufferedReader.
Feature Scanner BufferedReader
Speed Slow (scooter ≈) Fast (sports bike ≈)
Ease of use Beginner-friendly Moderate
Best for Small inputs / learning Large inputs / exams
Parsing Built-in nextInt() etc. Manual parseInt()
Exception No checked exception throws Exception needed
Famous Bug: Scanner nextLine() Trap
When you use nextInt() followed by nextLine(), the newline character is left in the
buffer. Always add an extra [Link]() after nextInt() to consume it.
2. Basic Single Value Inputs
2.1. Single Integer Input
Input
10
1 import [Link]. Scanner ;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 int n = sc. nextInt ();
7 System .out. println (" Input : " + n);
8 sc. close ();
9 }
10 }
Listing 1: Scanner — Single Integer
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) throws Exception {
5 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
6 int n = Integer . parseInt (br. readLine ());
7 System .out. println (" Input : " + n);
8 }
9 }
Listing 2: BufferedReader — Single Integer
2
Java Input Types — Complete Reference TCS NQT / Competitive Coding
2.2. Single String Input
Input
hello world
1 import [Link]. Scanner ;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 String str = sc. nextLine (); // reads entire line
7 System .out. println (" Input : " + str);
8 sc. close ();
9 }
10 }
Listing 3: Scanner — Single String
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) throws Exception {
5 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
6 String str = br. readLine ();
7 System .out. println (" Input : " + str);
8 }
9 }
Listing 4: BufferedReader — Single String
2.3. Single Double / Float Input
Input
12.34567
1 import [Link]. Scanner ;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 double num = sc. nextDouble ();
7 System .out. printf ("%.3f%n", num); // 3 decimal places
8 sc. close ();
9 }
10 }
Listing 5: Scanner — Double Input with formatted output
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) throws Exception {
5 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
6 double num = Double . parseDouble (br. readLine ());
7 System .out. printf ("%.3f%n", num);
3
Java Input Types — Complete Reference TCS NQT / Competitive Coding
8 }
9 }
Listing 6: BufferedReader — Double Input
Pro Tip
%.3f prints 3 decimal places. %.2f prints 2. This is important in output-formatting
problems.
2.4. Single Character Input
Input
a
1 import [Link]. Scanner ;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 char ch = [Link] (). charAt (0); // read token , take first char
7 System .out. println ("Char: " + ch);
8 sc. close ();
9 }
10 }
Listing 7: Scanner — Character Input
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) throws Exception {
5 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
6 char ch = br. readLine (). charAt (0);
7 System .out. println ("Char: " + ch);
8 }
9 }
Listing 8: BufferedReader — Character Input
3. Array Input Formats
3.1. Space-Separated Integer Array
Input
1 2 3 4 5
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 String line = sc. nextLine ();
4
Java Input Types — Complete Reference TCS NQT / Competitive Coding
7 String [] parts = line. split (" ");
8
9 int [] arr = new int[ parts . length ];
10 for (int i = 0; i < parts . length ; i++) {
11 arr[i] = Integer . parseInt ( parts [i]);
12 }
13
14 System .out. println ( Arrays . toString (arr));
15 sc. close ();
16 }
17 }
Listing 9: Scanner — Space-Separated Array
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
7 String line = br. readLine ();
8 String [] parts = line. split (" ");
9
10 int [] arr = new int[ parts . length ];
11 for (int i = 0; i < parts . length ; i++) {
12 arr[i] = Integer . parseInt ( parts [i]);
13 }
14
15 System .out. println ( Arrays . toString (arr));
16 }
17 }
Listing 10: BufferedReader — Space-Separated Array
3.2. Comma-Separated Integer Array
Input
1,2,3,4
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 String line = sc. nextLine ();
7 String [] parts = line. split (","); // only change vs
space - separated
8
9 int [] arr = new int[ parts . length ];
10 for (int i = 0; i < parts . length ; i++) {
11 arr[i] = Integer . parseInt ( parts [i]. trim ());
12 }
13
14 System .out. println ( Arrays . toString (arr));
15 sc. close ();
16 }
5
Java Input Types — Complete Reference TCS NQT / Competitive Coding
17 }
Listing 11: Scanner — Comma-Separated Array
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
7 String line = br. readLine ();
8 String [] parts = line. split (",");
9
10 int [] arr = new int[ parts . length ];
11 for (int i = 0; i < parts . length ; i++) {
12 arr[i] = Integer . parseInt ( parts [i]. trim ());
13 }
14
15 System .out. println ( Arrays . toString (arr));
16 }
17 }
Listing 12: BufferedReader — Comma-Separated Array
3.3. Bracket Format Array
Input
[1, 2, 3, 4]
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 String line = sc. nextLine ().trim ();
7 line = line. replaceAll (" \\[|\\] ", ""); // remove [ and ]
8
9 String [] parts = line. split (",");
10 int [] arr = new int[ parts . length ];
11 for (int i = 0; i < parts . length ; i++) {
12 arr[i] = Integer . parseInt ( parts [i]. trim ());
13 }
14
15 System .out. println ( Arrays . toString (arr));
16 sc. close ();
17 }
18 }
Listing 13: Scanner — Bracket Array
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
6
Java Input Types — Complete Reference TCS NQT / Competitive Coding
7 String line = br. readLine ().trim ();
8 line = line. replaceAll (" \\[|\\] ", "");
9
10 String [] parts = line. split (",");
11 int [] arr = new int[ parts . length ];
12 for (int i = 0; i < parts . length ; i++) {
13 arr[i] = Integer . parseInt ( parts [i]. trim ());
14 }
15
16 System .out. println ( Arrays . toString (arr));
17 }
18 }
Listing 14: BufferedReader — Bracket Array
3.4. Space-Separated String (Words) Array
Input
apple banana mango
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 String line = sc. nextLine ();
7 String [] words = line. split (" ");
8 System .out. println ( Arrays . toString ( words ));
9 sc. close ();
10 }
11 }
Listing 15: Scanner — String Array
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
7 String line = br. readLine ();
8 String [] words = line. split (" ");
9 System .out. println ( Arrays . toString ( words ));
10 }
11 }
Listing 16: BufferedReader — String Array
4. Number Input Patterns
7
Java Input Types — Complete Reference TCS NQT / Competitive Coding
4.1. First Line = Size, Second Line = Array (MOST COMMON in
TCS)
Input
5
1 2 3 4 5
1 import [Link] .*;
2
3 public class Main {
4 public static void inputWithSize ( Scanner sc) {
5 int n = sc. nextInt ();
6 sc. nextLine (); // IMPORTANT : consume leftover newline
7
8 String [] parts = sc. nextLine (). split (" ");
9 int [] arr = new int[n];
10
11 for (int i = 0; i < n; i++) {
12 arr[i] = Integer . parseInt ( parts [i]);
13 }
14
15 System .out. print (" Array (size given ): ");
16 for (int num : arr) {
17 System .out. print (num + " ");
18 }
19 System .out. println ();
20 }
21
22 public static void main( String [] args) {
23 Scanner sc = new Scanner ( System .in);
24 inputWithSize (sc);
25 sc. close ();
26 }
27 }
Listing 17: Scanner — Size + Array
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void inputWithSize ( BufferedReader br) throws Exception {
6 int n = Integer . parseInt (br. readLine ());
7
8 String [] parts = br. readLine (). split (" ");
9 int [] arr = new int[n];
10
11 for (int i = 0; i < n; i++) {
12 arr[i] = Integer . parseInt ( parts [i]);
13 }
14
15 System .out. print (" Array (size given ): ");
16 for (int num : arr) {
17 System .out. print (num + " ");
18 }
19 System .out. println ();
20 }
21
8
Java Input Types — Complete Reference TCS NQT / Competitive Coding
22 public static void main( String [] args) throws Exception {
23 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
24 inputWithSize (br);
25 }
26 }
Listing 18: BufferedReader — Size + Array
Scanner Trap
After [Link](), always call [Link]() once to consume the dangling newline
before reading the next line. This is one of the most common bugs in TCS exams.
4.2. Multiple Test Cases
Input
2
5
1 2 3 4 5
3
10 20 30
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 int t = sc. nextInt ();
7
8 while (t-- > 0) {
9 int n = sc. nextInt ();
10 sc. nextLine (); // consume newline
11
12 String [] parts = sc. nextLine (). split (" ");
13 int [] arr = new int[n];
14
15 for (int i = 0; i < n; i++) {
16 arr[i] = Integer . parseInt ( parts [i]);
17 }
18
19 System .out. println ( Arrays . toString (arr));
20 }
21 sc. close ();
22 }
23 }
Listing 19: Scanner — Multiple Test Cases
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
9
Java Input Types — Complete Reference TCS NQT / Competitive Coding
7 int t = Integer . parseInt (br. readLine ());
8
9 while (t-- > 0) {
10 int n = Integer . parseInt (br. readLine ());
11
12 String [] parts = br. readLine (). split (" ");
13 int [] arr = new int[n];
14
15 for (int i = 0; i < n; i++) {
16 arr[i] = Integer . parseInt ( parts [i]);
17 }
18
19 System .out. println ( Arrays . toString (arr));
20 }
21 }
22 }
Listing 20: BufferedReader — Multiple Test Cases
4.3. Mixed Input (int + String on separate lines)
Input
5
hello world
1 import [Link]. Scanner ;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 int n = sc. nextInt ();
7 sc. nextLine (); // MUST consume leftover newline
8
9 String str = sc. nextLine ();
10 System .out. println (n + " " + str);
11 sc. close ();
12 }
13 }
Listing 21: Scanner — Mixed Input
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) throws Exception {
5 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
6 int n = Integer . parseInt (br. readLine ());
7 String str = br. readLine ();
8 System .out. println (n + " " + str);
9 }
10 }
Listing 22: BufferedReader — Mixed Input
10
Java Input Types — Complete Reference TCS NQT / Competitive Coding
4.4. Array Input Without Size (Read Until EOF)
Input
1 2 3 4 5 (no size given, may span multiple lines)
1 import [Link] .*;
2
3 public class Main {
4 public static void inputWithoutSize ( Scanner sc) {
5 List <Integer > arr = new ArrayList < >();
6
7 while (sc. hasNextInt ()) {
8 [Link](sc. nextInt ());
9 }
10
11 System .out. print (" Array (size unknown ): ");
12 for (int val : arr) {
13 System .out. print (val + " ");
14 }
15 System .out. println ();
16 }
17
18 public static void main( String [] args) {
19 Scanner sc = new Scanner ( System .in);
20 inputWithoutSize (sc);
21 sc. close ();
22 }
23 }
Listing 23: Scanner — Array Without Size
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void inputWithoutSize ( BufferedReader br) throws Exception
{
6 List <Integer > arr = new ArrayList < >();
7 String line;
8
9 while (( line = br. readLine ()) != null && !line. isEmpty ()) {
10 String [] parts = line. split ("\\s+");
11 for ( String p : parts ) {
12 [Link]( Integer . parseInt (p));
13 }
14 }
15
16 System .out. print (" Array (size unknown ): ");
17 for (int val : arr) {
18 System .out. print (val + " ");
19 }
20 System .out. println ();
21 }
22
23 public static void main( String [] args) throws Exception {
24 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
25 inputWithoutSize (br);
26 }
11
Java Input Types — Complete Reference TCS NQT / Competitive Coding
27 }
Listing 24: BufferedReader — Array Without Size
Test Cases:
• Input: 1 2 3 4 5 → Output: Array (size unknown): 1 2 3 4 5
• Input (multi-line): 1 2 / 3 4 / 5 → Output: 1 2 3 4 5
• Input: -1 -2 3 4 → Output: -1 -2 3 4
• Input (Scanner stops at non-integer): 1 2 3 abc 4 5 → Output: 1 2 3
Feature Scanner BufferedReader
Stop condition Stops at invalid input Stops at EOF
Control Less More
Speed Slower Faster
5. String Input Patterns
5.1. Parsing Array from String Using split()
Input
10 20 abc 30 xyz 40 (mixed numeric and non-numeric)
1 import [Link] .*;
2
3 public class Main {
4 public static void inputAsString ( Scanner sc) {
5 String line = sc. nextLine ();
6 String [] tokens = line. split ("\\s+"); // split on any whitespace
7
8 List <Integer > arr = new ArrayList < >();
9 for ( String token : tokens ) {
10 try {
11 [Link]( Integer . parseInt ( token ));
12 } catch ( NumberFormatException e) {
13 // Ignore non - numeric values
14 }
15 }
16
17 System .out. print (" Array (from string ): ");
18 for (int val : arr) {
19 System .out. print (val + " ");
20 }
21 System .out. println ();
22 }
23
24 public static void main( String [] args) {
25 Scanner sc = new Scanner ( System .in);
26 inputAsString (sc);
27 sc. close ();
28 }
29 }
Listing 25: Scanner — String Parsing with Exception Handling
12
Java Input Types — Complete Reference TCS NQT / Competitive Coding
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void inputAsString ( BufferedReader br) throws Exception {
6 String line = br. readLine ();
7 String [] tokens = line. split ("\\s+");
8
9 List <Integer > arr = new ArrayList < >();
10 for ( String token : tokens ) {
11 try {
12 [Link]( Integer . parseInt ( token ));
13 } catch ( NumberFormatException e) {
14 // Ignore non - numeric values
15 }
16 }
17
18 System .out. print (" Array (from string ): ");
19 for (int val : arr) {
20 System .out. print (val + " ");
21 }
22 System .out. println ();
23 }
24
25 public static void main( String [] args) throws Exception {
26 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
27 inputAsString (br);
28 }
29 }
Listing 26: BufferedReader — String Parsing with Exception Handling
5.2. Extracting Numbers from Messy String (Most Powerful Pat-
tern)
Input
@12#34$56 or Marks: 45, 78, 90 or 10 abc 20 xyz 30
1 import [Link] .*;
2
3 public class Main {
4 public static void mostUsedInputFormat ( Scanner sc) {
5 String line = sc. nextLine ();
6
7 List <Integer > arr = new ArrayList < >();
8 StringBuilder numStr = new StringBuilder ();
9
10 for (char ch : line. toCharArray ()) {
11 if ( Character . isDigit (ch)) {
12 numStr . append (ch);
13 } else if (ch == '-' && numStr . length () == 0) {
14 // handle negative numbers
15 numStr . append (ch);
16 } else if ( numStr . length () > 0) {
17 [Link]( Integer . parseInt ( numStr . toString ()));
13
Java Input Types — Complete Reference TCS NQT / Competitive Coding
18 numStr . setLength (0);
19 }
20 }
21
22 // catch last number ( important !)
23 if ( numStr . length () > 0 && ! numStr . toString (). equals ("-")) {
24 [Link]( Integer . parseInt ( numStr . toString ()));
25 }
26
27 System .out. print (" Array ( extracted numbers ): ");
28 for (int val : arr) {
29 System .out. print (val + " ");
30 }
31 System .out. println ();
32 }
33
34 public static void main( String [] args) {
35 Scanner sc = new Scanner ( System .in);
36 mostUsedInputFormat (sc);
37 sc. close ();
38 }
39 }
Listing 27: Scanner — Number Extractor (Advanced)
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void mostUsedInputFormat ( BufferedReader br) throws
Exception {
6 String line = br. readLine ();
7
8 List <Integer > arr = new ArrayList < >();
9 StringBuilder numStr = new StringBuilder ();
10
11 for (char ch : line. toCharArray ()) {
12 if ( Character . isDigit (ch)) {
13 numStr . append (ch);
14 } else if (ch == '-' && numStr . length () == 0) {
15 numStr . append (ch);
16 } else if ( numStr . length () > 0) {
17 [Link]( Integer . parseInt ( numStr . toString ()));
18 numStr . setLength (0);
19 }
20 }
21
22 if ( numStr . length () > 0 && ! numStr . toString (). equals ("-")) {
23 [Link]( Integer . parseInt ( numStr . toString ()));
24 }
25
26 System .out. print (" Array ( extracted numbers ): ");
27 for (int val : arr) {
28 System .out. print (val + " ");
29 }
30 System .out. println ();
31 }
32
33 public static void main( String [] args) throws Exception {
34 BufferedReader br = new BufferedReader (new
14
Java Input Types — Complete Reference TCS NQT / Competitive Coding
InputStreamReader ( System .in));
35 mostUsedInputFormat (br);
36 }
37 }
Listing 28: BufferedReader — Number Extractor
Test Cases:
• Input: 1 2 3 4 → Output: 1 2 3 4
• Input: 10 abc 20 xyz 30 → Output: 10 20 30
• Input: @12#34$56 → Output: 12 34 56
• Input: 12345 → Output: 12345
• Input: -10 20 -30 → Output: -10 20 -30
5.3. Extracting Digits from Continuous String
Input
12345 (treat each digit as separate element)
1 import [Link]. Scanner ;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 String s = sc. nextLine ();
7
8 for (int i = 0; i < s. length (); i++) {
9 int digit = s. charAt (i) - '0';
10 System .out. print ( digit + " ");
11 }
12 sc. close ();
13 }
14 }
Listing 29: Scanner — Digit Extraction
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) throws Exception {
5 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
6 String s = br. readLine ();
7
8 for (int i = 0; i < s. length (); i++) {
9 int digit = s. charAt (i) - '0';
10 System .out. print ( digit + " ");
11 }
12 }
13 }
Listing 30: BufferedReader — Digit Extraction
15
Java Input Types — Complete Reference TCS NQT / Competitive Coding
6. ArrayList Input
6.1. Using ArrayList Instead of Array
ArrayList is preferred when size is unknown at compile time.
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 String line = sc. nextLine ();
7 String [] parts = line. split (" ");
8
9 List <Integer > list = new ArrayList < >();
10 for ( String p : parts ) {
11 [Link]( Integer . parseInt (p));
12 }
13
14 System .out. println (" ArrayList : " + list);
15 sc. close ();
16 }
17 }
Listing 31: Scanner — ArrayList Input (space-separated)
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
7 String line = br. readLine ();
8 String [] parts = line. split (" ");
9
10 List <Integer > list = new ArrayList < >();
11 for ( String p : parts ) {
12 [Link]( Integer . parseInt (p));
13 }
14
15 System .out. println (" ArrayList : " + list);
16 }
17 }
Listing 32: BufferedReader — ArrayList Input
6.2. ArrayList of Strings
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 int n = sc. nextInt ();
7 sc. nextLine ();
8
9 List <String > list = new ArrayList < >();
10 for (int i = 0; i < n; i++) {
16
Java Input Types — Complete Reference TCS NQT / Competitive Coding
11 [Link](sc. nextLine ());
12 }
13
14 System .out. println (list);
15 sc. close ();
16 }
17 }
Listing 33: Scanner — ArrayList of Strings
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
7 int n = Integer . parseInt (br. readLine ());
8
9 List <String > list = new ArrayList < >();
10 for (int i = 0; i < n; i++) {
11 [Link](br. readLine ());
12 }
13
14 System .out. println (list);
15 }
16 }
Listing 34: BufferedReader — ArrayList of Strings
7. 2D Array Input
7.1. Method 1: Flat Comma-Separated Input (fill row-wise)
Input
1, 2, 3, 4, 5, 6 (rows = 3, cols = 2)
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 int rows = 3, cols = 2;
7
8 String line = sc. nextLine ();
9 String [] parts = line. split (",");
10
11 int [][] matrix = new int[rows ][ cols ];
12 int k = 0;
13
14 for (int i = 0; i < rows; i++) {
15 for (int j = 0; j < cols; j++) {
16 matrix [i][j] = Integer . parseInt ( parts [k]. trim ());
17 k++;
18 }
19 }
17
Java Input Types — Complete Reference TCS NQT / Competitive Coding
20
21 for (int i = 0; i < rows; i++) {
22 System .out. println ( Arrays . toString ( matrix [i]));
23 }
24 sc. close ();
25 }
26 }
Listing 35: Scanner — 2D Array from flat input
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
7 int rows = 3, cols = 2;
8
9 String line = br. readLine ();
10 String [] parts = line. split (",");
11
12 int [][] matrix = new int[rows ][ cols ];
13 int k = 0;
14
15 for (int i = 0; i < rows; i++) {
16 for (int j = 0; j < cols; j++) {
17 matrix [i][j] = Integer . parseInt ( parts [k]. trim ());
18 k++;
19 }
20 }
21
22 for (int i = 0; i < rows; i++) {
23 System .out. println ( Arrays . toString ( matrix [i]));
24 }
25 }
26 }
Listing 36: BufferedReader — 2D Array from flat input
7.2. Method 2: Row-by-Row Matrix Input (standard format)
Input
2 3
1 2 3
4 5 6
1 import [Link] .*;
2
3 public class Main {
4 public static void main( String [] args) {
5 Scanner sc = new Scanner ( System .in);
6 int r = sc. nextInt ();
7 int c = sc. nextInt ();
8
9 int [][] arr = new int[r][c];
10
18
Java Input Types — Complete Reference TCS NQT / Competitive Coding
11 for (int i = 0; i < r; i++) {
12 for (int j = 0; j < c; j++) {
13 arr[i][j] = sc. nextInt ();
14 }
15 }
16
17 System .out. println ( Arrays . deepToString (arr));
18 sc. close ();
19 }
20 }
Listing 37: Scanner — Row-by-Row Matrix
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void main( String [] args) throws Exception {
6 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
7
8 String [] rc = br. readLine (). split (" ");
9 int r = Integer . parseInt (rc [0]);
10 int c = Integer . parseInt (rc [1]);
11
12 int [][] arr = new int[r][c];
13
14 for (int i = 0; i < r; i++) {
15 String [] row = br. readLine (). split (" ");
16 for (int j = 0; j < c; j++) {
17 arr[i][j] = Integer . parseInt (row[j]);
18 }
19 }
20
21 System .out. println ( Arrays . deepToString (arr));
22 }
23 }
Listing 38: BufferedReader — Row-by-Row Matrix
Pro Tip
Use [Link](arr[i]) for row-by-row printing and
[Link](arr) to print the entire matrix at once.
8. Advanced TCS Input Patterns
8.1. First or Last Element is Array Size
Possible Inputs
5 1 2 3 4 5 (first = size) OR 1 2 3 4 5 5 (last = size)
1 import [Link] .*;
2
3 public class Main {
19
Java Input Types — Complete Reference TCS NQT / Competitive Coding
4 public static void solve ( Scanner sc) {
5 String line = sc. nextLine ();
6 String [] tokens = line. split ("[^0 -9]+"); // split by non -digit
7
8 List <Integer > list = new ArrayList < >();
9 for ( String t : tokens ) {
10 if (!t. isEmpty ()) {
11 [Link]( Integer . parseInt (t));
12 }
13 }
14
15 int n = [Link] ();
16 List <Integer > arr = new ArrayList < >();
17
18 // Case 1: first element == remaining count
19 if ([Link] (0) == n - 1) {
20 for (int i = 1; i < n; i++) [Link]([Link](i));
21 }
22 // Case 2: last element == count before it
23 else if ([Link](n - 1) == n - 1) {
24 for (int i = 0; i < n - 1; i++) [Link]([Link](i));
25 }
26 // Case 3: no size indicator
27 else {
28 arr = list;
29 }
30
31 System .out. println (arr);
32 }
33
34 public static void main( String [] args) {
35 Scanner sc = new Scanner ( System .in);
36 solve (sc);
37 sc. close ();
38 }
39 }
Listing 39: Scanner — Detect and handle size as first/last element
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static void solve ( BufferedReader br) throws Exception {
6 String line = br. readLine ();
7 String [] tokens = line. split ("[^0 -9]+");
8
9 List <Integer > list = new ArrayList < >();
10 for ( String t : tokens ) {
11 if (!t. isEmpty ()) [Link]( Integer . parseInt (t));
12 }
13
14 int n = [Link] ();
15 List <Integer > arr = new ArrayList < >();
16
17 if ([Link] (0) == n - 1) {
18 for (int i = 1; i < n; i++) [Link]([Link](i));
19 } else if ([Link](n - 1) == n - 1) {
20 for (int i = 0; i < n - 1; i++) [Link]([Link](i));
21 } else {
20
Java Input Types — Complete Reference TCS NQT / Competitive Coding
22 arr = list;
23 }
24
25 System .out. println (arr);
26 }
27
28 public static void main( String [] args) throws Exception {
29 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
30 solve (br);
31 }
32 }
Listing 40: BufferedReader — Detect size as first/last element
Test Cases:
• Input: 5 1 2 3 4 5 → Output: [1, 2, 3, 4, 5]
• Input: 1 2 3 4 5 5 → Output: [1, 2, 3, 4, 5]
• Input: 2,5,4,3,65 → Output: [2, 5, 4, 3, 65] (no match)
• Input: 1345-200 → Output: [1345, 200]
8.2. Check Equal Average (Extract Digits from String)
Input
2222 (check if any split of digit-array yields equal averages)
1 import [Link] .*;
2
3 public class Main {
4 public static boolean checkEqualAverage ( String s) {
5 List <Integer > arr = new ArrayList < >();
6
7 for (char c : s. toCharArray ()) {
8 if ( Character . isDigit (c)) {
9 [Link](c - '0'); // char to int
10 }
11 }
12
13 if ([Link] () < 2) return false ;
14
15 int totalSum = arr. stream (). mapToInt ( Integer :: intValue ).sum ();
16 int currSum = 0;
17
18 for (int i = 0; i < [Link] () - 1; i++) {
19 currSum += [Link](i);
20 int currSize = i + 1;
21 int remSum = totalSum - currSum ;
22 int remSize = [Link] () - currSize ;
23
24 // Compare averages without floats : sum1* size2 == sum2* size1
25 if ( currSum * remSize == remSum * currSize ) {
26 return true;
27 }
28 }
29
30 return false ;
21
Java Input Types — Complete Reference TCS NQT / Competitive Coding
31 }
32
33 public static void main( String [] args) {
34 Scanner sc = new Scanner ( System .in);
35 String s = sc. nextLine ();
36 sc. close ();
37 System .out. println ( checkEqualAverage (s) ? "True" : "False ");
38 }
39 }
Listing 41: Scanner — Check Equal Average
1 import [Link] .*;
2 import [Link] .*;
3
4 public class Main {
5 public static boolean checkEqualAverage ( String s) {
6 List <Integer > arr = new ArrayList < >();
7
8 for (char c : s. toCharArray ()) {
9 if ( Character . isDigit (c)) {
10 [Link](c - '0');
11 }
12 }
13
14 if ([Link] () < 2) return false ;
15
16 int totalSum = arr. stream (). mapToInt ( Integer :: intValue ).sum ();
17 int currSum = 0;
18
19 for (int i = 0; i < [Link] () - 1; i++) {
20 currSum += [Link](i);
21 int currSize = i + 1;
22 int remSum = totalSum - currSum ;
23 int remSize = [Link] () - currSize ;
24
25 if ( currSum * remSize == remSum * currSize ) return true;
26 }
27
28 return false ;
29 }
30
31 public static void main( String [] args) throws Exception {
32 BufferedReader br = new BufferedReader (new
InputStreamReader ( System .in));
33 String s = br. readLine ();
34 System .out. println ( checkEqualAverage (s) ? "True" : "False ");
35 }
36 }
Listing 42: BufferedReader — Check Equal Average
Core Trick: Avoid Float Precision Errors
Instead of comparing avg1 == avg2, use cross-multiplication:
sum1 × size2 = sum2 × size1
This avoids floating-point precision bugs entirely.
22
Java Input Types — Complete Reference TCS NQT / Competitive Coding
Test Cases:
• 2222 → True (any split gives avg = 2)
• 1233 → False
• 1122 → False
9. Quick Reference Cheat Sheet
Input Format Key Method Notes
Single integer nextInt() / Most basic
parseInt(readLine())
Single double nextDouble() / Use %.3f for output
parseDouble()
Single string nextLine() / Full line
readLine()
Single character next().charAt(0) /
readLine().charAt(0)
Space-separated array split(" ") Most common in TCS
Comma-separated ar- split(",") Same logic, different sep
ray
Bracket array replaceAll + Remove [ ] first
split(",")
Word array split(" ") No parsing needed
Size + array nextInt() + Famous trap: consume \n
nextLine()
Multiple test cases t loop Very common in TCS
Mixed input int then nextLine() Always consume newline
No-size array hasNextInt() / EOF Rare but tricky
loop
Messy string numbers Manual char scan Most powerful pattern
Flat → 2D matrix split + nested loop Row-wise fill
Row-by-row matrix readLine().split() Standard matrix
per row
First/Last = size split("[^0-9]+") + Edge case
check
ArrayList [Link]() in loop When size unknown
23
Java Input Types — Complete Reference TCS NQT / Competitive Coding
Golden Rule for Any Weird Input
1. Read full line as String
2. Identify separator (" ", ",", [ ])
3. Call split()
4. Convert with [Link]()
No overthinking needed!
TCS Most Asked Formats
Space-separated array
Size + array (two lines)
String input / mixed input
Multiple test cases
! Sometimes bracket format
! Rarely: size as first/last element
24