My code is runnning well in eclipse but here it is showing error

import java.util.Scanner;

public class Chhalengeofbinarysearch {

public static void main(String[] args) {

	int[] array = takesinput();
	System.out.println("enter the value of iteam");
	Scanner scn = new Scanner(System.in);
	int iteam = scn.nextInt();
	int nilesh = returnindex(array,iteam);
	System.out.println(nilesh);
}

 //takesinput function
public static int[] takesinput() {
	System.out.println("enter the size of array");
	Scanner scn = new Scanner(System.in);
	int N = scn.nextInt();

	int[] arr = new int[N];
	for (int i = 0; i < arr.length; i++) {
		System.out.println("enter the value for" + i + "index in asending or decending order");
		arr[i] = scn.nextInt();
	}
	return arr;    
		}

// returnindex function
public static int returnindex(int [] arr ,int iteam) {
	int lo =0;
	int higher= arr.length ;
	while(lo<=higher) {
	int mid =(lo+higher)/2;
	if(arr[mid]<iteam) {
		lo=mid+1;
	}
	else if (arr[mid]>iteam) {
		higher=mid-1;
	}
	else
		return mid;
	}
	return -1;
	
}

}

and output is
compiling failed with exitcode 1,…why?

@Nilesh-Kumar-1173882276149322,
You are creating 2 Scanner class objects. That is causing the errors. https://ide.codingblocks.com/s/172428 I have corrected the errors. Also, you don’t need to print the statements you have printed such as System.out.println(“enter the value of iteam”);

I have one dout why u have passed Scanner scn to takesinput function i.e
takesinput(Scanner scn )
and in main function when u call takesinput function u passed scn to takesinput function like takesinput(scn) why???

plzz clear my daut in video lecture if we decleare Scanner globaly their is no need to pass scanner in any function

I have one dout why u have passed Scanner scn to takesinput function i.e
takesinput(Scanner scn )
and in main function when u call takesinput function u passed scn to takesinput function like takesinput(scn) why???

@Nilesh-Kumar-1173882276149322,

Scanner scn is our object of scanner class. If we define it globally then we don’t need to create the object of scanner class again and again. The scope of global scanner class object is throughout the function.

In Scanner scn = new Scanner(System.in); you need to make it static to make it a class variable or pass the object of scanner class to takeinput function like I have done if you don’t make it static.