Wrong Answer

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! */

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

/* Name of the class has to be “Main” only if the class is public. */
class Codechef{

public static void main(String[] args) throws java.lang.Exception {
	Scanner in = new Scanner(System.in);
	double balance = 120.00;
	double withdraw = 0;
	double fee = 0.50;

	```System.out.println("Your current balance is: $" + balance + "\n");```
	```System.out.println("Enter the ammount you wish to withdraw: ");```
	```withdraw = in.nextDouble();```

	```if (withdraw % 5 == 0) {```
		```balance -= withdraw - fee;```
		```System.out.println("$" + balance);```
	```} else if (balance < withdraw) {```
		```System.out.println("Insufficient funds: $" + balance);```
	} else {
		```System.out.println("The ammount chosen isn't a multiple of $5. Your balance is: $" + balance);```
	}

}

}

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.

2 Likes

This means instead of the above line just print balance. Similarly for other cases too.

2 Likes

I’m still getting a wrong answer

public static void main(String[] args) throws java.lang.Exception {
	Scanner in = new Scanner(System.in);
	double balance = 120.00;
	double withdraw = 0;
	double fee = .50;
	
  withdraw = in.nextDouble();
	  ```System.out.print(balance);```
	

	  if (withdraw % 5 == 0) {
		balance -= withdraw - fee;
		```System.out.println(balance);```
	} else if (balance < withdraw) {
		```System.out.println(balance);```
	} else {
		```System.out.println(balance);```
	}

}

}

 withdraw = in.nextDouble();
	  ```System.out.print(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.

That helped, but I still get the following:

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??

When I ran your program, I didn’t get that. What input did you try?

Also, CodeChef’s IDE is not much different from other IDEs, so you can test your program locally and just submit when it’s finished.

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);

I mean the input itself, not the code.

Such as 30 120.00

Something stupid, because I didn’t understand you put the input in the box. I literally put my code in there. I’m getting closer, I think.

I’m trying to comment a smiley face, but it’s screaming at me for not having a complete sentence.

So, (: