Results 1 to 6 of 6

Thread: getAccountIndex

  1. #1
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default getAccountIndex

    public static String getAccountIndex(String accountNumber){

    ...

    }

    This method accept an account number and find that account's index in the parallel arrays (you will only need to search the array of account numbers). Since the arrays are parallel, an account will have the same index in each array. Keep in mind that the account number is NOT the account's index in the arrays!

    I want to create a method that searches for a particular index in an array using an account number that the user can input. I am unsure how to approach this.

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Ping

    I considered a reply to this request,
    but I kept coming up with simpler and
    simpler algorithms to the point that I
    thought, "anthonym2121 must have
    figured this out already."

    So now, I am reduced to pinging
    anthonym2121 to find out, "Do you
    still need help with this method?"
    Last edited by paul pasciak; 04-01-2009 at 08:46 PM.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Uh... Is this what you mean?

    Java Code:
    public static int getAccountIndex(int accountNumber)
    {
    	for(int i = 0; i < parallelArray.length; i++)
    	{
    		if(accountNumber == parallelArray[i])
    		return i;
    	}
    	return -1;
    }

  4. #4
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by AndrewM16921 View Post
    Uh... Is this what you mean?

    Java Code:
    public static int getAccountIndex(int accountNumber)
    {
    	for(int i = 0; i < parallelArray.length; i++)
    	{
    		if(accountNumber == parallelArray[i])
    		return i;
    	}
    	return -1;
    }
    I think this is it except for the fact that accountNumber is not an int it is a string but that should not matter...I will try it out

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Well, just remember you can't use == when comparing strings. Good luck. : )

  6. #6
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by AndrewM16921 View Post
    Well, just remember you can't use == when comparing strings. Good luck. : )
    I was thinking of having two parameter: one being the string, and the other an array of strings. Since I am comparing between two strings I used ".equals". The following errors in this code appear:

    Error: accountNumber is already defined in getAccountIndex [line:99]

    Error: incompatible types [line: 103]
    found : java.lang.String
    required: java.lang.String[]


    Java Code:
        [B][line:99][/B] public static int getAccountIndex(String accountNumber, String accountNumber[]){
           int result = -1;
           
           
           [B][line:103][/B] accountNumber = JOptionPane.showInputDialog("What is your account number?");
           
           for(int i=0;i<accountNumber.length;i++){
            
             if(accountNumber.equals(accountNumber[i])){
              return i; 
             }
           }
           return result;
         }

Posting Permissions

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