Challenge 2 Problem

The code that I wrote in Eclipse is running perfectly but the same code while I am trying to run here it is showing error.
The error is Exception in thread “main” java.util.NoSuchElementException.

My code is
Scanner scn=new Scanner(System.in);

int n=scn.nextInt();

	int i,num=0,m=n-1;
	for(i=0;i<n;i++){
		int a=2;
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		num=num+N*(int)Math.pow(a,m);
		m--;
		//System.out.println(+num);
}
	System.out.println(+num);

P.S. I have imported the Math library for using the power function.

@VinayakSingh11111,
https://ide.codingblocks.com/s/251463 corrected code.

Algo:

  • The idea is to extract the digits of given binary number starting from right most digit and keep a variable decvalue.
  • At the time of extracting digits from the binary number, multiply the digit with the proper base (Power of 2)
  • and add it to the variable dec_value.
  • At the end, the variable decvalue will store the required decimal number.

Also, don’t use 2 different scanner objects. Only use 1 scanner object in the entire program.

No, I just want to know what is the cause of the error?

@VinayakSingh11111,

Don’t use 2 different scanner objects. Only use 1 scanner object in the entire program and that 1 scanner object should be common for all inputs

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.