Bilal Alam
Bilal Alam

Reputation: 894

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

Upvotes: 0

Views: 349

Answers (1)

Nitesh Shakya
Nitesh Shakya

Reputation: 91

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.

Upvotes: 1

Related Questions