Error in run time

I am getting this error in run time :-

Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)

Code :-

import java.util.*;
import java.util.LinkedList;
public class Main
{
public static class GenericTree
{
private class Node
{
int data;
ArrayList children;
Node()
{
this.children=new ArrayList<>();
}
Node(int data)
{
this.data=data;
this.children=new ArrayList<>();
}
}
private Node root;
private int size;
private static Scanner sc=new Scanner(System.in);

GenericTree()
{
	this.root=takeInput(null);
}
private Node takeInput (Node parent)
{
	Node n=new Node(sc.nextInt());
	this.size++;
	
	int children=sc.nextInt();
	
	for(int i=1;i<=children;i++)
	{
		Node child=takeInput(n);
		n.children.add(child);
	}
	 	return n;
}

public int levelsum(int target)
{
    LinkedList <Node> queue=new LinkedList<>();
    int level=0;
       queue.add(root);
       if(target==0)
       return root.data;
       
       while(!queue.isEmpty())
       {
           int size=queue.size();
           
           if(level==target)
           {
               int sum=0;
               while(!queue.isEmpty())
               {
                   sum+=(queue.remove()).data;
               }
               return sum;
           }
           while(size-->=0)
           {
               Node node=queue.remove();
               for(int j=0;j<node.children.size();j++)
	           {
	               queue.add(node.children.get(j));
	           }
	           size--;
           }
           level++;
       }
       return 0;
    }

}
public static void main(String[] args)
{
Scanner sc=new Scanner (System.in);
GenericTree g=new GenericTree();

	int k=sc.nextInt();
	System.out.print(g.levelsum(k));
}

}

@tishachhabra2702_8fc5a68a2e295e35 It’s a common error. Check the elements you want as input and you’re asking for more input than is given in the testcase. Also refer this function to see how to take input for this question.

static Node buildTree(Node root) {
        int d = sc.nextInt();
        int n = sc.nextInt();
        root = new Node(d);
        if (n == 0) {
            return root;
        } else if (n == 1) {
            root.left = buildTree(root.left);
            return root;
        } else {
            root.left = buildTree(root.left);
            root.right = buildTree(root.right);
            return root;
        }
    }

I tried modifying using your function but it didn’t work. Please help!

import java.util.*;
import java.util.LinkedList;
public class Main
{
public static class GenericTree
{
private class Node
{
int data;
Node left;
Node right;
Node()
{}
Node(int data)
{
this.data=data;
}
}
private Node root;
private int size=0;
private static Scanner sc=new Scanner(System.in);

GenericTree()
{
	this.root=buildTree(null);
}
private Node buildTree(Node root) 
{
    int d = sc.nextInt();
    int n = sc.nextInt();
    root = new Node(d);
    if (n == 0) {
        return root;
    } else if (n == 1) {
        root.left = buildTree(root.left);
        return root;
    } else {
        root.left = buildTree(root.left);
        root.right = buildTree(root.right);
        return root;
    }
}
public int levelsum(int target)
{
    LinkedList <Node> queue=new LinkedList<>();
    int level=0;
       queue.add(root);
       if(target==0)
       return root.data;
       
       while(!queue.isEmpty())
       {
           int size=queue.size();
           
           if(level==target)
           {
               int sum=0;
               while(!queue.isEmpty())
               {
                   sum+=(queue.remove()).data;
               }
               return sum;
           }
           while(size-->=0)
           {
               Node node=queue.remove();
               if(!(node.left==null))
	               queue.add(node.left);
	           if(!(node.right==null))
	               queue.add(node.right);
	           
	           size--;
           }
           level++;
       }
       return 0;
    }

}
public static void main(String[] args)
{
Scanner scn=new Scanner (System.in);
GenericTree g=new GenericTree();
int k=scn.nextInt();
System.out.print(g.levelsum(k));
}
}

@tishachhabra2702_8fc5a68a2e295e35 you should not create another scanner in your main function. Use your global scanner bcoz making another scanner confuses your program as to which thing to scan. Solution : make your scanner sc global for main class such that you can use that in genericTree class and also in main Function. Corrected code : (look for the comment just below main class initiation). ALSO even after correcting this you will testcase which looks like :
20 2
40 1
204 1
65 1
24 0
60 2
92 1
21 0
65 1
85 0
3. Debug your code for this testcase(your task). correct output = 171. Your output = 151

Also you can refer fully working code here :

1 Like

Thanks, it really helped!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.