0

I'm stupid.

import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double withdraw = scanner.nextDouble();
double balance = scanner.nextDouble();
int withdraw = 0; int balance;
if (withdraw % 5 == 0 && withdraw<(balance-.5)) {
    balance = balance - (withdraw + .5); 
    System.out.println(balance);
}
 else {
     System.out.println(balance);
 }}}

I'm trying to make it so that the Balance is being subtracted by the Withdrawal amount while incurring a $.50 charge. Unfortunately, it keeps only subtracting the $.50 without subtracting withdraw. Thanks in advance.

FIXED CODE

import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double withdraw = scanner.nextDouble();
double balance = scanner.nextDouble();
if (withdraw % 5 == 0 && withdraw<(balance-.5)) {
    balance -= (withdraw + .5); 
    System.out.println(balance);
}
 else {
     System.out.println(balance);
 }}}
6
  • 2
    Have you checked that withdraw != 0 ?
    – MrSmith42
    Jan 18, 2013 at 23:45
  • 1
    There's no way to debug it - please post ALL your code!
    – Nir Alfasi
    Jan 18, 2013 at 23:45
  • 2
    Your equation looks correct. There must be something you're not showing us! Q: Are "balance" and "withdraw" defined as floating point, or as integer?
    – paulsm4
    Jan 18, 2013 at 23:46
  • 2
    Some more code, please? What does withdraw print?
    – Jimmy C
    Jan 18, 2013 at 23:46
  • print balance and withdraw before substracting and cut-paste all output Jan 18, 2013 at 23:47

3 Answers 3

2

Here is the algorithm https://stackoverflow.com/a/14387552/1083704

You have the same problem - a bug in the complex expression, which includes using unknown types. And you must do the same thing to debug your program -- simplify the oneliner into multiple simple expressions, using intermediate variables. Then you can step-by-step debug your code and observe those intermediate values. Being a programmer means being a hacker and you won't be a hacker without learning debugging.

5
  • 2
    -1 - How is that even relevant? And always try to include the relevant portions, to prevent link-rot from deleted questions. Jan 19, 2013 at 0:41
  • @Clockwork-Muse: I agree. I just don't feel like penalizing myself a point by voting down such a silly response. I gotta wonder - is Val making a joke? Or just on drugs?
    – paulsm4
    Jan 19, 2013 at 0:55
  • It is silly to do debug in the response instead of teaching user to do it himself, meta.stackexchange.com/questions/105582/… I do not mind to recievce downwotes from silly people, who do not understand this and prefer drawing SO in high entropy instead.
    – Val
    Jan 19, 2013 at 9:09
  • 1
    The case there is only different by < operator instead of - here and that is the whole difference. Likewise there, user does not bother to simplify the problematic expression by introducing intermediate values. You urge to do the work for himself and punish the idea of SO, which is reusability -- to serve as a textbook for programming practices. If that answer is irrelevant for this then you cannot learn from SO at all and it is useless. You demonstrate that it is useless and help turning it into a dump of garbage and punish those who pursue the reusalbility of ideas.
    – Val
    Jan 19, 2013 at 9:46
  • The idea to copy-paste answers in case SO pages can be deleted also seems defeating the sharing purpose of SO. There is a practice that duplicate questions are closed. Might be they should not, because there are no guarantees that the first question will not be deleted. Right? Do not mind that I show that we have a duplicate here.
    – Val
    Jan 19, 2013 at 11:10
0

where are you setting the withdraw variable? I'd look there, it sounds like you're adding .5 to a variable that hasn't been assigned a value greater than 0. A real easy way to test in more complex code would be to set withdraw in your code to say a value of 5 right before using it in your balance equation, that way you can tell if the problem is with your equation, or the withdraw variable, the equation looks solid though. Also, you can do

balance -= (withdraw + .5)
0
0

It is fairly hard to debug code when given so little information; but my best guess would be an initialization bug - where withdraw has not been initialized correctly and is just at what I'd assume to be its default value, 0

But to be sure, we'd need to know what withdraw is equal to before your print statement. Could you add a print statement before adding up the balance like so:

System.out.println(withdraw);
balance -= (withdraw + .5);
System.out.println(balance);

And then see whether withdraw is > 0 at runtime? And if not; check that it gets set to your withdraw value before computing the balance.

Even better would be to post your full code snippet, so we can see ourselves I guess.

N.B. a -= b is shorthand for a = a - b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.