The issue many students face in the TCS NQT with Java isn't usually the logic—it's the
"Input
Buffet." If you expect one format and they give you another, your code crashes before it even
runs.
Here are the 3 most common input variations you might encounter and how to handle them:
Variation 1: The "Comma-Separated" Array
Sometimes, instead of spaces, TCS provides the array with commas (e.g., 0,1,2,4,5,6,7).
● The Issue: [Link]() looks for spaces by default. It will read the whole string 0,1,2... as
a single token and throw a NumberFormatException.
● The Fix: Tell your StringTokenizer to look for commas.
Java
// Use this if you see commas in the sample input
StringTokenizer st = new StringTokenizer([Link](), ",");
Variation 2: The "Brackets and Commas" Input
This is the most frustrating one. You might get [0, 1, 2, 4, 5, 6, 7].
● The Issue: The [ and ] characters will crash [Link]().
● The Fix: Clean the string before tokenizing it.
Java
String line = [Link]().trim();
line = [Link]("[", "").replace("]", "").replace(",", " ");
StringTokenizer st = new StringTokenizer(line);
Variation 3: No "N" Provided
TCS occasionally skips the line that tells you the size of the array (N). They just give you the
array directly.
● The Issue: Your int[] nums = new int[n] will fail because you don't have n.
● The Fix: Read the line, tokenize it, and use a dynamic list or count the tokens.
Java
String line = [Link]();
StringTokenizer st = new StringTokenizer(line);
int n = [Link](); // This tells you how many numbers are there
int[] nums = new int[n];
for(int i = 0; i < n; i++) {
nums[i] = [Link]([Link]());
}
Why Java Students specifically face "Compiler Issues":
1. Multiple Classes: If you have more than one public class in a single file, it will fail. Only the
class with main should be public.
2. Static Method Context: Students often forget to make their logic function static (like your
public static int findMissingElement), so they can't call it from the main method without
creating an object.
3. The "Main" Requirement: As we discussed, if the compiler expects the class name to be
Main and you name it Q2FindMissingElement, it won't even [Link]: Always rename your
class to Main before final submission unless the editor already has a class structure
provided.
My Advice for Exam Day
Read the Sample Input box very carefully.
● If you see: 5 \n 1 2 3 4 5 -> Your current code is perfect.
● If you see: 1, 2, 3, 4, 5 (only one line) -> Use the countTokens() and comma-delimiters trick.
Would you like me to give you a "Safe Template" that handles both spaces and commas so
you're protected against both?