 <p>You can find the extra braces by making use of stack as below:</p> <pre><code>public static void main(final String[] args) { Stack&lt;String&gt; stack = new Stack&lt;String&gt;(); File file = new File("InputFile"); int lineCount = 0; try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { lineCount++; for (int i = 0; i &lt; line.length(); i++) { if (line.charAt(i) == '{') { stack.push("{"); } else if (line.charAt(i) == '}') { if (!stack.isEmpty()) { stack.pop(); } else { System.out.println("Extra brace found at line number : " + lineCount); } } } } if (!stack.isEmpty()) { System.out.println(stack.size() + " braces are opend but not closed "); } } catch (Exception e) { e.printStackTrace(); } } </code></pre>