Java.util.NoSuchElement Exception

import java.util.Scanner;
class Codechef
{
  public static void main (String[] args)
  {
   Scanner in=new Scanner(System.in);
   int T= in.nextInt();

   for(int k=0;k<T;)
   {
     int M=in.nextInt();
     int N=in.nextInt();
     int product=M*N;
     Double result=0.0;
     for(int i=2;i<product/2;i++)
     {
       if((product%Math.pow(i,2))==0)
       {
        result=product/Math.pow(i,2);
       }
     }
     System.out.println(result);
    }
  }
} 

Input:

2

10 15

9 3

Output:

6.0

3.0

Error:

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 Codechef.main(Main.java:18)

My program actually prints the minimum squares that can be made from the given length*breadth dimension my code is working fine when i am taking input once but i want to take as many inputs as the user wants,for this i have applied for loop before taking input M and N but it throws runtime error now although it works fine without that for loop in line:15 any help would be appreciated.thanks in advance

Answers


I see that your loop will be infinite, as you haven't increased your counter. And since that is an infinite loop it exhausts your input, which is the reason for that exception.

add k++ in the first loop, like you have i++ in the second loop. That should fix it.


Need Your Help

how to setup sites framework in django

python django django-models django-sites

I am trying to setup multiple sites from a single project in django. I was trying to set this up following https://docs.djangoproject.com/en/1.8/ref/contrib/sites/ but there is no mention of setu...

FindBugs not accepting bcel.jar in ANT script

java ant findbugs task bcel

I installed findbugs into my ant lib directory and added the following code into my main ANT script: