ATM: Java Experts, getting rte

,

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

class atm
{
public static void main(String args[])
{
	Scanner sc=new Scanner(System.in);
	float balance,withdraw,cbalance;
		withdraw=sc.nextInt();
		balance=sc.nextInt();
	if(withdraw%5==0)
		{
			cbalance=(balance-withdraw)-0.50f;
			if(cbalance<0)
				System.out.format("%.2f%n",balance);
				else
				System.out.format("%.2f%n",cbalance);
		}	
	else
{
System.out.println(balance);
}
}
}

I’m Having a run time error in this.I’m a new to this code chef i don’t why this is providing error.

I ran your code at IDEONE.com, and your solution throws the following exception:

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:840)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at Main.main(Main.java:12)

So ,

obviously,you are getting a runtime error due to mismatch in input.

Just Read the problem statement again especially the Input format section :

*First Input:Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.

Second Input:Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja’s initial account balance.*

So ,the second input must be a floating point number.

But your code takes the second input as an Integer.

 balance=sc.nextInt();//Instead of float ,you are taking integer Input :(

So,this is where the Mismatch occur and yes,that’s the reason ,you are getting a Runtime Error.

How to Recover?

Just Replace(Correct) the line balance=nextInt() with

balance=sc.nextFloat();

The rest of the things are absolutely Fine .

3 Likes

Thanks for your solution the link you gave was very use full…

@mr_manoj its my pleasure:) :slight_smile:

//