-2

The following code works fine with the eclipse but then in online editor I keep receiving runtimeException(NoSuchElementFoundException) please help me where am I going wrong?

{I have used sieve of eratosthenes alogirthm to find prime number in required range by the user}

    public class test {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    if(sc.equals(""))
    {
        sc.close();
    }
    Integer t = sc.nextInt();
    while(t>0)
    {
    int m = sc.nextInt();
    int n = sc.nextInt();
    int prime[]= new int[n+1];
    for(int i=0;i<=n;i++)
    {
        prime[i]=1;
    }
    prime[0]=0;
    prime[1]=0;
    for(int i=2;i<Math.sqrt(n);i++)
    {
        if(prime[i]==1)
        {
            for(int j=2;i*j<n;j++)
            {
                prime[i*j]=0;
            }
        }
    }
    for(int i=m;i<n;i++)
    {
        if(prime[i]==1)
        {
            System.out.println(i+" ");
        }
    }
    System.out.println();
    t--;
   }
    }
   }
Kirthi
  • 13
  • 2
  • Well where are you going wrong ... maybe you should provide some input to your online editor? – Tom Nov 03 '19 at 16:50
  • `prime[1]=0` This line assumes that n is at least 1. If the user enters something less than 1, then a `NoSuchElementFoundException` will be thrown. – mario_sunny Nov 03 '19 at 16:50
  • okay so basically it has testcases which has input values – Kirthi Nov 03 '19 at 16:51
  • But more than likely the `NoSuchElementFoundException` originates from the nested for loop. Have you tried debugging? – mario_sunny Nov 03 '19 at 16:52
  • No idea what you are trying to do with the terminating condition `i – mario_sunny Nov 03 '19 at 16:53
  • @mario_sunny prime[1]=0 is outside the loop and it is part of the algorithm to make 0th and 1st element as 0 as they are not prime numbers. – Kirthi Nov 03 '19 at 16:53
  • You should check which line it says the error is in. I suspect the scanner. You should check for `hasNext()`, not for `""`. And the rest of the code should be in `else`, as there is no point in using the scanner after it's closed. – RealSkeptic Nov 03 '19 at 16:56
  • *"it has testcases which has input values"* ... what is "it"? What are these testcases and what are these input values? – Tom Nov 03 '19 at 16:56
  • @mario_sunny Since when does accessing an invalid array index yield a `NoSuchElementFoundException`? – Tom Nov 03 '19 at 16:58
  • @RealSkeptic I wanted to give a condition that if the input value is empty then don't continue further. Can you help me with how do I do this? – Kirthi Nov 03 '19 at 17:00
  • @Tom Basically its a question on SPOJ to generate prime number which I am trying to solve – Kirthi Nov 03 '19 at 17:01
  • `sc` is a Scanner, not a string. You are comparing it to a string. That doesn't make sense. At the point where you are doing it, you don't have any input, but you can check if there is any input waiting by using `sc.hasNext()` and in fact you should do it before using any `sc.next...` call, and stop operations if it's false. – RealSkeptic Nov 03 '19 at 17:03
  • @RealSkeptic I tried to improve the condition by giving if(sc.hasNext()) { t=0} else { t = sc.nextInt() } But this doesn't seem to work. I made t =0 so that it does nothing and get out which is expected output. But it still keep giving exception. – Kirthi Nov 03 '19 at 17:10
  • That condition doesn't make sense. You ask if there is input. If ___yes___, set t to 0, if ___no___ try to read the non-existing input. What else than the exception should happen when you try to read something that doesn't exist? – Tom Nov 03 '19 at 17:14
  • @Tom sorry its the other way round. if there is input then continue further else make t =0 – Kirthi Nov 03 '19 at 17:18
  • Please update your question with the current code that you are running. Note: If there is no input, you should *stop* the program. Not do anything, not calculate anything. So what you do is always proceed only if there *is* input. – RealSkeptic Nov 03 '19 at 17:19
  • Hello Kirthi, well, your code is bit hard to follow, however it worked on my intellij.. I think the problem is with your online editor, which editor do you use? btw, welcome to both worlds: stackoverflow and java – Hasnaa Ibraheem Nov 03 '19 at 19:19
  • @Hasnaa Ibraheem thank you. It's spoj. It's a competitive programing webpage. – Kirthi Nov 04 '19 at 10:39

1 Answers1

0

I tried your code over the site you mentioned. I changed the class visibility to be default to avoid some compilation error. It produced internal error with no further details and it suggested to test the code on https://ideone.com/ site. I used the sample test case and it passed successfully on ideone site..
I suggest that you use hacker rank, I think it is more handy. In hacker rank you can unlock test cases to trace errors, bugs in your code, also you can have discussion with colleagues there over the problem.
On the other hand I couldn't get the exception you mentioned "NoSuchElementFoundException" if you have detailed stack trace kindly include it and I shall update my answer accordingly.

Hasnaa Ibraheem
  • 1,132
  • 1
  • 10
  • 18