Results 1 to 6 of 6
Thread: If statement help
- 10-27-2012, 04:12 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 10
- Rep Power
- 0
If statement help
My if statement in my Transaction class checks to see if the account number and sort code entered by the user is correct, then they can proceed to make the payment, else an error message pops up. However every time i enter the correct account number and sort code which I previously entered in my Account class the error message still pops up :s.Java Code:public class Transaction { // instance variables - replace the example below with your own private Integer amount; private Integer balance; /** * Constructor for objects of class Transaction */ public Transaction() { // initialise instance variables balance = 0; amount = null; } public void makePayment(Account acRef, Integer AccountNumber, Integer SortCode, Integer paymentAmount, String paymentTo) { if((AccountNumber == acRef.getAccountNumber())&&(SortCode == acRef.getSortCode())){ //compares account number and sort code entered in the parameter to the account number and sort code currently // held in the Account class through .getAccountNumber amount = paymentAmount; String place = paymentTo; if(balance <= 0){ System.out.println("You do not have sufficient funds to make this transaction"); }else{ balance = balance - amount; System.out.println("Payment of £" + amount + "to " + place); } }else{ System.out.print("Incorrect account number or sort code"); } }
Help please
Last edited by Manny123; 10-27-2012 at 04:17 PM.
- 10-27-2012, 04:20 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: If statement help
If you're comparing strings use .equals() and not ==
- 10-27-2012, 04:30 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 10
- Rep Power
- 0
Re: If statement help
But my account number parameter is an Integer so how would i use .equals()
- 10-27-2012, 05:02 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: If statement help
Now that you edited it you can't use .equals() anymore. ;)
Besides that I can't really see a problem with the if-statement.
- 10-28-2012, 12:11 AM #5
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: If statement help
we'd have to see the Account class... I would ask why you're using Integer instead of the primitive int. Is this a requirement? Integer is an Object, so has a .equals(). Luckily someone overrode the Object.equals() with one that checks integer values and not memory location. :)
If you run this:
you'll see that x == y is not true! Use the primitive int! (or .equals)Java Code:Integer x = new Integer(2); Integer y = new Integer(2); int a = 2; int b = 2; if(x.equals(y)){ System.out.println("x equals y"); // this will print } if(x == y){ System.out.println("x=y"); // this will NOT print } if(a == b){ System.out.println("a=b"); // this will print }Last edited by SJF; 10-28-2012 at 12:38 AM. Reason: added comments
- 10-28-2012, 12:30 AM #6
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: If statement help
... To add the confusion:
Bottom Line: avoid Integer, Double, Float, Boolean objects unless you really cannot!Java Code:Integer x = new Integer(2); int a = 2; if(x == 2){ System.out.println("x = 2"); // this prints } if(x == a){ System.out.println("x=a"); // this prints! }
Edit: Reasons to use Integer include (but are not limited to) using anywhere a generic is needed i.e. ArrayList<Integer> as ArrayList<int> will not work!Last edited by SJF; 10-28-2012 at 12:40 AM. Reason: comments/clarification
Similar Threads
-
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 04:27 PM -
Need Help For doing if else statement
By juvenchan in forum New To JavaReplies: 11Last Post: 02-27-2011, 05:22 AM -
for statement help
By helpisontheway in forum New To JavaReplies: 5Last Post: 11-14-2009, 04:14 PM -
if statement help please!!
By soc86 in forum New To JavaReplies: 5Last Post: 12-02-2008, 02:56 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks