NoSuchElementException error

I am getting a NoSuchElementException error in the following code while running it here but I am not getting any error when I run it on eclipse.

Any help would be appreciated .

problem : http://www.codechef.com/problems/HS08TEST

code:

import java.util.;
import java.io.
;

public class atm

{

public static void main(String[] args) 

{


	double w,c;



	Scanner input=new Scanner(System.in);

	w=input.nextDouble();  //it is giving error at this line

	c=input.nextDouble();

	if(w%5==0&&w<=(c+.5))

	{


		c=c-w-.5;

		System.out.printf("%.2f",c);

	}

	else if(w%5==0&&w>c)

		System.out.printf("%.2f",c);

	else if(w%5!=0)

		System.out.printf("%.2f",c);



}

}

just check if X is a multiple of 5, and Pooja’s account balance has enough cash to perform the withdrawal transaction(including bank charges).

If the above condition is true then c = c - w - .5, else c = c remains unchanged no need for the two elseif’s.

Make the above changes and you will get AC. :slight_smile:

Here is your updated code.

1 Like

http://ideone.com/23reDo
rjohari23, sir, the above mentioned link, in which there’s your version of code, even that is giving me NoSuchExceptionError in the line no.11 w=input.nextDouble();

thank you so much . but why was that error generated if you could share .

@itsmesiddhant if we don’t check that the file has next line before reading it, then it throws exception after reading last line since it is trying to read a line which doesn’t exist. This is one of the reasons for this error.

Thank you again . It is people like you that make people like me better.Thanks a lot !!

NoSuchElementException: This is a Java specific error, when your code tries to read input more than available, such an Exception is thrown. Use of hasNext family of methods is advisable.

The best way (according to me) to get rid of this error is to wrap our code in the condition scanner.hasNextInt(). i.e. putting an if (scanner.hasNextInt()) before asking for input from the user and wrapping up the whole code in it except the scanner.close() part.