Results 1 to 6 of 6
  1. #1
    Manny123 is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Question If statement help

    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");
    }
    }
    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.

    Help please
    Last edited by Manny123; 10-27-2012 at 05:17 PM.

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: If statement help

    If you're comparing strings use .equals() and not ==

  3. #3
    Manny123 is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Default Re: If statement help

    But my account number parameter is an Integer so how would i use .equals()

  4. #4
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default 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.

  5. #5
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default 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:
    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
    		 }
    you'll see that x == y is not true! Use the primitive int! (or .equals)
    Last edited by SJF; 10-28-2012 at 01:38 AM. Reason: added comments

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: If statement help

    ... To add the confusion:

    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!
    		 }
    Bottom Line: avoid Integer, Double, Float, Boolean objects unless you really cannot!
    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 01:40 AM. Reason: comments/clarification

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  2. Need Help For doing if else statement
    By juvenchan in forum New To Java
    Replies: 11
    Last Post: 02-27-2011, 06:22 AM
  3. for statement help
    By helpisontheway in forum New To Java
    Replies: 5
    Last Post: 11-14-2009, 05:14 PM
  4. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 03:56 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •