I am able to execute the following code in ubuntu but not on hackerrank platform. My output is correct, however I am unable to move forward because of a runtime error :
Exception in thread "main" java.util.NoSuchElementException
import java.util.Scanner;
import java.io.*;
class Solution
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t=in.nextInt();
int s=0;
for(int i=0;i<=t;i++)
{
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
s=a;
for(int j=0;j<n;j++)
{
s+= b * Math.pow(2,j);
System.out.print(s+" ");
}
System.out.println();
}
in.close();
}
}
java.util.NoSuchElementException is thrown when there is no next element, To avoid this you should check by using hasNextInt().
Try this:
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t= 0;
if(in.hasNextInt())
t=in.nextInt();
int s=0;
for(int i=0;i<=t;i++)
{
int a = 0;
int b = 0;
int n = 0;
if(in.hasNextInt())
in.nextInt();
if(in.hasNextInt())
b = in.nextInt();
if(in.hasNextInt())
n = in.nextInt();
s=a;
for(int j=0;j<n;j++)
{
s+= b * Math.pow(2,j);
System.out.print(s+" ");
}
System.out.println();
}
in.close();
}