I am not sure what I’ve done wrong. Can someone help, please?
Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja’s account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja’s account balance after an attempted transaction.
Input
Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.
Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja’s initial account balance.
Output
Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.
/* package codechef; // don’t place package name! */
You need to output the answer in the exact format as specified by the problem. Check the Output block to know the format. You should not print Insufficient funds: $" + balance.
You are outputting balance here and then again below.
Also, you are not inputting the initial amount of balance and instead declaring balance=120 which will not be true for all cases.
This means you need to get both withdraw and balance from input.
I think you can figure out the rest from here.
Read Editorialist’s Solution in C++
I am about at my wits ends. I still get a wrong answer, plus I get the following when trying to run my program:
Runtime Error
NZEC
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.nextDouble(Scanner.java:2413)
at Codechef.main(Main.java:18)
public static void main(String[] args) throws java.lang.Exception
{ Scanner in = new Scanner(System.in); double balance = 0; double withdraw = 0; double fee = 0.50;
System.out.println("Please enter your balance.");
balance = in.nextDouble();
System.out.print(balance + "\t");
System.out.println("Please enter an amount to withdraw.");
withdraw = in.nextDouble();
System.out.print(balance);
if (withdraw % 5 == 0 && balance > (withdraw + fee)) {
balance -= withdraw - fee;
System.out.println(balance);
} else if (balance < withdraw) {
System.out.println(balance);
} else {
System.out.println(balance);
}
}
}
Look at your error. Scanner is throwing a NoSuchElementException in the method nextDouble(). This means it’s trying to read something, but isn’t finding anything. The reason for this? When “running” the program on CodeChef’s IDE, you have to manually paste in the input for the program. There’s a checkbox for that next to the run button.
Exception in thread “main” java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at Codechef.main(Main.java:18)
Why can’t CodeChef’s IDE be like Eclipse or NetBeans??
public static void main(String[] args) throws java.lang.Exception
{ Scanner in = new Scanner(System.in); double balance = 0; double withdraw = 0;
System.out.println("Please enter your balance.");
balance = in.nextDouble();
System.out.print(balance);
System.out.println("Please enter an amount to withdraw.");
withdraw = in.nextDouble();
System.out.print(balance);