Sum At Level K : No such element Exception

import java.util.*;
public class Main {
public static void main(String args[]) {

	Tree tree = new Tree();
	Scanner s = new Scanner(System.in);

	int k = s.nextInt();

	System.out.println(tree.sumAtLevelK(k));

	
}


public static class Tree{

	private class Node
	{
		int data;
		ArrayList <Node> children;

		Node(int data)
		{
			this.data = data;
			children = new ArrayList <> ();
		}
	}


	private Node root;


	Tree()
	{
		Scanner s = new Scanner(System.in);
		this.root = buildTree(s);
	}


	private Node buildTree(Scanner s)
	{
		int data = s.nextInt();

		Node node = new Node(data);

		int number = s.nextInt();

		for(int i = 0; i<number; i++)
		{
			Node child = buildTree(s);
			node.children.add(child);
		}

		return node;
	}


	

	public int sumAtLevelK(int k)
	{
		return this.sumAtLevelK(this.root, k);
	}
	private int sumAtLevelK(Node node, int k)
	{
		if(node == null)
			return 0;
		
		int level = 0;
		int sum = 0;
		int size = 0;

		Queue <Node> q = new LinkedList <> ();
		q.add(node);

		while(!q.isEmpty())
		{
			size = q.size();

			while(size-- != 0)
			{
				if(level == k)
				{
					sum += q.poll().data;
				}
				else
				{
					Node temp = q.poll();
					for(int i = 0; i<temp.children.size(); i++)
						q.add(temp.children.get(i));
				}
			}

			level++;
		}

		return sum;
	}
}

}

Hey @arjunsabu99
Code is fine
make static scanner :

import java.util.*;

public class Main {
static Scanner s = new Scanner(System.in);
public static void main(String args[]) {

	Tree tree = new Tree();
//	Scanner s = new Scanner(System.in);

	int k = s.nextInt();

	System.out.println(tree.sumAtLevelK(k));

}

public static class Tree {

	private class Node {
		int data;
		ArrayList<Node> children;

		Node(int data) {
			this.data = data;
			children = new ArrayList<>();
		}
	}

	private Node root;

	Tree() {
		
		this.root = buildTree();
	}

	private Node buildTree() {
		int data = s.nextInt();

		Node node = new Node(data);

		int number = s.nextInt();

		for (int i = 0; i < number; i++) {
			Node child = buildTree();
			node.children.add(child);
		}

		return node;
	}

	public int sumAtLevelK(int k) {
		return this.sumAtLevelK(this.root, k);
	}

	private int sumAtLevelK(Node node, int k) {
		if (node == null)
			return 0;

		int level = 0;
		int sum = 0;
		int size = 0;

		Queue<Node> q = new LinkedList<>();
		q.add(node);

		while (!q.isEmpty()) {
			size = q.size();

			while (size-- != 0) {
				if (level == k) {
					sum += q.poll().data;
				} else {
					Node temp = q.poll();
					for (int i = 0; i < temp.children.size(); i++)
						q.add(temp.children.get(i));
				}
			}

			level++;
		}

		return sum;
	}
}

}

what is wrong in using two Scanners? one inside the tree class and one in main. Why did you make it static

your code Will check
if there are any more integers left in input.
then you got
Exception in thread β€œmain” java.util.NoSuchElementException