0% found this document useful (0 votes)
111 views1 page

TCS NQT Java Syntax & Error Guide

The document is a comprehensive guide for Java syntax and error handling specifically for TCS NQT, outlining environment rules, common compilation errors, input/output formats, and important syntax reminders. It includes details on string methods, type conversions, array and collection handling, and formatting output. Additionally, it provides examples of math methods, conditional statements, and character operations, along with a sample full template for coding.

Uploaded by

ankushpatidar18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views1 page

TCS NQT Java Syntax & Error Guide

The document is a comprehensive guide for Java syntax and error handling specifically for TCS NQT, outlining environment rules, common compilation errors, input/output formats, and important syntax reminders. It includes details on string methods, type conversions, array and collection handling, and formatting output. Additionally, it provides examples of math methods, conditional statements, and character operations, along with a sample full template for coding.

Uploaded by

ankushpatidar18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TCS NQT Java Complete Syntax and Error Handling Guide

Prepared for: Ankush Patidar

1. Environment Rules - Use class name Main only.


- No package declarations allowed.
- Avoid printing prompts like "Enter number:".
- Input/Output through Scanner (preferred) or BufferedReader.
- No file handling or console class.
2. Common Compilation Errors ■ Wrong class name → always use Main.
■ Remove package line.
■ Never write public before other classes.
■ Count braces properly before submitting.
3. Input Format Rules Single integer input:
int n = [Link](); Multiple in one line:
int a = [Link](); int b = [Link](); String input:
String s = [Link](); After nextInt():
[Link](); to consume newline.
4. Output Format Rules - Don’t print extra text like “Sum is”.
- Exact format as question.
5. Forgettable Syntax - Class name: class Main { ... }
- Main method: public static void main(String[] args)
- String length: [Link]()
- Char at: [Link](i)
- String compare: [Link](str2)
- Array length: [Link]
- Loop: for(int i=0; i - 2D Array: for(i)... for(j)...
6. Common String Methods toUpperCase(), toLowerCase(), substring(), contains(), split(), charAt(), length()
Split safely: String[] parts = [Link]().split("\\s+");
Comma split: [Link](",");
7. Type Conversions String → int: [Link](str)
String → double: [Link](str)
int → String: [Link](n)
char → int: (int)c, int → char: (char)n
8. Array & Collections int[] arr = new int[n]; [Link](arr); [Link]([Link](arr)); ArrayList:
ArrayList list = new ArrayList<>(); [Link](10); [Link](0); [Link](); 9. Formatting Output Print till 2 decimals:
[Link]("%.2f", val); Rounding:
[Link](val * 100.0) / 100.0 10. Math Methods [Link](), [Link](), [Link](), [Link](), [Link](),
[Link](), [Link]()
11. Conditional & Loops Ternary: int max = (a > b) ? a : b;
Enhanced for-loop: for(int x : arr)
12. StringBuilder Tricks Reverse a string:
StringBuilder sb = new StringBuilder(str); [Link](); 13. Modulo & Negative Numbers int mod = ((a % b) + b) %
b; 14. Floating Comparison if ([Link](a - b) < 1e-9) 15. Character Operations char c = 'A'; int ascii = (int)c; char
next = (char)(c + 1); 16. Frequency Count Example int[] freq = new int[26]; for(char c : [Link]()) freq[c -
'a']++; 17. Average of Array int sum = 0; for(int x : arr) sum += x; double avg = (double)sum / [Link];
[Link]("%.2f", avg); 18. Important Imports import [Link].*; import [Link].*; 19. Sample Full Template
import [Link].*; class Main { public static void main(String[] args) { Scanner sc = new Scanner([Link]); int n =
[Link](); int[] arr = new int[n]; for(int i=0;i

Common questions

Powered by AI

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+").

You might also like