I am trying to run the below code which does not work when int numTest = sc.nextInt() is used whereas works fine when int numTest = Integer.parseInt(sc.nextLine()) is used. But I am trying to get only an integer value here with which I can get the number of string set from user. So why can't I use sc.nextInt()?
public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        **try{
          int numTest = Integer.parseInt(sc.nextLine());
          for(int i = 0; i < numTest; i++){
              String ch = sc.nextLine();
           if(isBalanced(ch)){
               System.out.println("YES");
           } else {
               System.out.println("NO");
           }
          }**
        } catch(Exception e){
            System.out.println("Invalid Input");
        }
    }
    public static boolean isBalanced(String s){
        if(s == null || s.length() % 2 != 0) return false;
        Stack<Character> stack = new Stack<Character>();
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if(c == '(' || c == '{' || c == '['){
                stack.push(c);
            } else if(c == ')' || c == '}' || c == ']'){
                if(!stack.isEmpty()){
                    char latestOpenP = stack.pop();
                    if(latestOpenP == '(' && c != ')'){
                        return false;
                    } else if(latestOpenP == '{' && c != '}'){
                        return false;
                    } else if(latestOpenP == '[' && c != ']'){
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
    
        return stack.isEmpty();
    }
}
 
     
    