d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Help With Java
Add Reply New Topic New Poll
Member
Posts: 588
Joined: Apr 29 2009
Gold: 143.76
Dec 17 2012 07:17pm
Okay well, i need to make a program to withdraw an indicated (user-input) amount from a bank account. Set the beginning balance as $10,000 and set up the program so that if the withdrawal amount is less than 0 or greater than $10,000 it will trigger an assertion error but i am having some troubles.
I have this:

import java.util.Scanner;

public class Project2
{
public static void main(String args[])
{


Scanner input = new Scanner(System.in);

System.out.print("Available balance: $10,000. Please input the amount you want to withdraw: ");
int num = input.nextInt();

assert(num <= 0 || num >= 10000):
System.out.printf("Invalid amount", num);
}
}

But when i enter a number that is less then zero it does not give me my assertion error, and when i put in a number larger then 10,000 it also does not give my assertion error. What am i doing wrong?
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Dec 17 2012 08:27pm
Printf is formatted wrong. You need a %XXX-Reserved number of decimals. --> /e Thought you want to show Invalid amount XX :P

/e I have never seen Assert being used. Use if >.<

This post was edited by IsraeliSoldier on Dec 17 2012 08:33pm
Member
Posts: 588
Joined: Apr 29 2009
Gold: 143.76
Dec 17 2012 09:24pm
lol i would, but sadly i can't for this project. lol i am still confused
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 17 2012 09:34pm
to answer your question, i think assertions are disabled by default. you need to use the -enableassertions option.

you're abusing assertions. code that you write when assertions fail should never execute in the final product. you only use them to make things clear to developers (pre/post conditions, invariants) or find bugs.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll