0

I am using an online ide geeksforgeeks. Here, I am trying to solve this question using TreeSet and passing a Comparator object. The qusetion is as follow:

You are given an array A of size N. Replace every element with the next greatest element (greatest element on its right side) in the array. Also, since there is no element next to the last element, replace it with -1.

Do this for t test cases: Here's the code I have written:

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    static Scanner sc=new Scanner(System.in);
    public static void main (String[] args) {
        //code
        int t=sc.nextInt();
            for(int i=0;i<t;i++)
                display();
    }
    static void display(){
        int n=sc.nextInt();
        Set<Integer> ts=new TreeSet<Integer>(new myComparator());
        int a;
        for(int i=0;i<n;i++){
            a=sc.nextInt();
            ts.add(a);
        }
        Iterator itr=ts.iterator();
        int count=0;
        while(itr.hasNext()){
            if(count==0)
                continue;
            else{
                System.out.print(itr.next()+" ");
            }
        }
        System.out.print(-1);
        System.out.println();
    }
}
class myComparator implements Comparator<Integer>{
    public int compare(Integer obj1,Integer obj2){
        if(obj2>obj1)
            return 1;
        else if(obj2<obj1)
            return -1;
        else 
            return 0;
    }
}

The error that my code throws:

Exception in thread "main" 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 GFG.display(File.java:20)
    at GFG.main(File.java:13)

Please rectify the code. Code

dFuse_mArv
  • 13
  • 5

3 Answers3

0

java.util.NoSuchElementException is only possible when the input console is exhausted.

Apart from that if(count==0) will always be true in the above code and else block will never be executed.

QuickSilver
  • 3,613
  • 2
  • 9
  • 26
0

Could not reproduce NoSuchElementException, however, your code contains endless loop as you never consume elements in the iterator:

while(itr.hasNext()){
    if(count==0)
        continue;
    else {
       System.out.print(itr.next()+" ");
    }
}

Assuming that this issue is resolved, and the code runs, there are other issues:

  • using Set causes loss of input data
  • using reverse comparator changes the order of output
  • your code does not seem to produce output array containing -1

Upon applying these fixes and testing

Iterator itr=ts.iterator();
int count=0;
while(itr.hasNext()){
    if (count == 0) {
        itr.next();
        count++;
    } else {
        System.out.print(itr.next() + " ");
    }
}
System.out.print(-1);
System.out.println("\nEND" + ts);

the following results have been retrieved:

input n:

5

20 20 30 40 40

30 20 -1
END[40, 30, 20]
Alex Rudenko
  • 15,656
  • 9
  • 13
  • 33
0

Its because your for loop in display() iterates to n which is the value of the element, not the number of elements found.

So in your code

int n=sc.nextInt();

'n' becomes the value 887

for(int i=0;i<n;i++){

Iterates 84 times (the number of elements passed in), then throws this exception because it cannot go any further (trying to get to 887). Hence

java.util.NoSuchElementException

Because there are no more elements left to get to using nextInt()

james
  • 633
  • 1
  • 13