0

I'm new to java and trying CodeChef problems but every time I get error of nonsuchelementexception, I tried to do it with another method but it gave the some error ,here is one example please someone help me out

 class newexp
    {
        static int[] bubbleSort(int arr[])
        {
            int n = arr.length;
            for (int i = 0; i < n-1; i++)
                for (int j = 0; j < n-i-1; j++)
                    if (arr[j] > arr[j+1])
                    {
                        // swap arr[j+1] and arr[i] 
                        int temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
            return arr;
        }
        static int search (int s , int arr[]){
            int a[] = arr;
            int start = 0;
            int n = a.length;
            int end = n-1;
            while(start<=end){
                int mid = start+end/n;
                if(arr[mid]<s){
                    start++;
                }
                else if(arr[mid]>s){
                    end--;
                }
                else{
                    return mid;
                }
            }
            return -1;
        }

        public static void main (String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            for (int i = 0;i<t;i++){
                int num = sc.nextInt();
                int a[] = new int[num];
                for(int j =0;j<num;j++){
                    a[j] = sc.nextInt();

                }
                int s = sc.nextInt();
                int b = a[s];
                bubbleSort(a);
                System.out.println(search(b , a));
            }
        }
    }

java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at newexp.main(Main.java:47)

(Nothing to read about the problem is that much it id for the requirement to post the problem please don't mind it )

Aman Rao
  • 11
  • 1

0 Answers0