TCS NQT Java Syntax & Error Guide
TCS NQT Java Syntax & Error Guide
To compare two floating-point numbers effectively, use if (Math.abs(a - b) < 1e-9) to account for potential precision errors in floating point arithmetic .
Common string methods include toUpperCase(), toLowerCase(), length(), charAt(), substring(), contains(), and split(). They allow manipulation through changing case, finding length, accessing characters, segmenting strings, and checking for substrings .
Character operations involve converting chars to ints to get ASCII values and vice versa. For example, char c = 'A'; int ascii = (int)c; gets the ASCII value of a character, and you can get the next character with char next = (char)(c + 1).
Common errors include using incorrect class names, missing braces, or incorrect use of public in classes other than Main. Address these by always using the class name Main, double-checking brace counts, and removing unnecessary public accessors .
Examples of type conversions include: String to int using Integer.parseInt(str), String to double using Double.parseDouble(str), int to String with String.valueOf(n), char to int with (int)c, and int to char using (char)n .
For array input, first declare the array with int[] arr = new int[n] and then read elements using a loop. Frequency counting can be achieved using an int array sized according to the problem, typically 26 for alphabets, and updating the count using freq[c - 'a']++ for each character .
To handle precision, use Math.round(val * 100.0) / 100.0 for rounding and System.out.printf("%.2f", val) for formatting output to two decimal places .
The key rules include naming the class as Main, using the main method signature as public static void main(String[] args), and avoiding package declarations .
For arrays, typical for-loops are used. For enhanced readability and simplicity, an enhanced for-loop is recommended, such as for(int x : arr) for arrays and collections .
A string can be split safely by trimming it first and then splitting using a regular expression like \s+ for spaces using String[] parts = s.trim().split("\\s+").