search array and find index

using search within array

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2867 Views - Last Post: 08 December 2010 - 07:22 PM Rate Topic: -----

#1 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

search array and find index

Posted 08 December 2010 - 09:48 AM

Right I am wrtiting a method that will search an array and return the index of that array based on its account number which is passed through as string.

Here is the method.

It will use a method in another class called BankAccount which has a getAccountNumber method.

here is the code:

public int findAccount(String accountNumber)
    
        
  {  
   
        int index = 0;
        for ( BankAccount ba: accounts)
       {
            if( ba.getAccountNumber().equals(accountNumber))        
            return index;             
            index++;
             
         
              }
         
            return index -1;
    }
   
       



Is This A Good Question/Topic? 0
  • +

Replies To: search array and find index

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,395
  • Joined: 27-December 08

Re: search array and find index

Posted 08 December 2010 - 09:52 AM

What exactly is your question?
Was This Post Helpful? 0
  • +
  • -

#3 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: search array and find index

Posted 08 December 2010 - 09:57 AM

View Postmacosxnerd101, on 08 December 2010 - 08:52 AM, said:

What exactly is your question?


the above method just returns null pointer exception for some reason and wont work. It works fine in an Arraylist
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,395
  • Joined: 27-December 08

Re: search array and find index

Posted 08 December 2010 - 09:59 AM

Post more code, as well as the Stack Trace.
Was This Post Helpful? 0
  • +
  • -

#5 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: search array and find index

Posted 08 December 2010 - 10:14 AM

View Postmacosxnerd101, on 08 December 2010 - 08:59 AM, said:

Post more code, as well as the Stack Trace.



Here is the BankAccount class

public class BankAccount
{
    private Name accountName;
    private Address accountAddress;
    private int balance;  // as pence
    private static int accountNumberGenerator = 1001;
    private String accountNumber;
    

    /**
     * Constructor for BankAccount
     * @param number account number
     * @param name account name
     */
    public BankAccount(String firstName, String lastName,
                       String addr1, String addr2, String addr3, String postcode, double openingBalance)
    {
        accountNumber = "" + accountNumberGenerator++;
        accountName = new Name(firstName, lastName);
        accountAddress = new Address (addr1, addr2, addr3, postcode);
        balance = convertToPence(openingBalance);
    } 
    
    // accessors (queries)

    /**
     * @return the account number
     */
    public String getAccountNumber()
    { 
        return accountNumber;
    } 

    /**
     * @return the account name
     */
    public String getAccountName()
    { 
        return accountName.getFullName();
    } 
    
    /**
     * @return the account address
     */
    public String getAccountAddress()
    {
        return accountAddress.getFullAddress();
    }

    /**
     * @return the balance in the account
     */
    public String getBalance()
    { 
        return penceToPounds(balance);
    } 
    
    // mutators 
    
     /**
     * adds an amount to the balance
     * @param amount the amount to add to balance
     */
    public void deposit(double amount)
    { 
        balance = balance + convertToPence(amount);
    } 

    /**
     * withdraws a given amount from the account
     * @param amount the amount to withdraw
     * @return the new balance
     */
    public void withdraw(double amount)
    { 
        if (balance - convertToPence(amount) <= 0)
        {
            System.out.println("Transaction refused: Insufficient funds.");
        }
        else
        {
            balance = balance - convertToPence(amount);
        }
    } 

    // other methods
    private int convertToPence(double amount)
    {
        return (int)(100*amount);
    }
    
    private String penceToPounds(int pence)
    {
      int pounds;
      int remainingPence;
      String result;    
      pounds = pence / 100;
      remainingPence = pence % 100;    
      result = "£"+pounds+".";    
      if (remainingPence < 10)
      {
        result = result+"0"+remainingPence;
      }
      else
      {
         result = result+remainingPence;
      }       
      return result;
    } 
    
     /**
     * Indicates whether some other object is "equal to" this one.
     * Checks for matches of accountNumber
     * @return true if a match found otherwise false
     */
    public boolean equals(Object o)
    {
        if ( o == this )
            return true;
        BankAccount account = (BankAccount)o;
        if ( (accountNumber.equals(account.getAccountNumber())))
            return true;
        return false;    
    }
    
    /**
     * @return all data values as a formatted string
     */
    public String toString()
    {
        String output = accountName.getFullName() +"\n" + accountAddress.getFullAddress() + "\n"
                        + "Account Number: " + accountNumber + "\tBalance: " + getBalance();
        
        return output;
    }

}  




and this is my code so for ignore the empty methods as they need to call the findAccount method to updte the relevant account.

public class BankAccountArray
{
    private BankAccount [] accounts;
    private int nextAccountIndex;
    public BankAccountArray()
    {
        accounts = new BankAccount [10];
        nextAccountIndex = 0;
    }
    public void openBankAccount(String firstName, String lastName,
    String addr1, String addr2, String addr3, String postcode, double openingBalance)
    {
        if (nextAccountIndex < accounts.length)
        {
            accounts[nextAccountIndex] = (new BankAccount(firstName, lastName, addr1, addr2, addr3, postcode, openingBalance));
            nextAccountIndex++;
            
        }
        else
       System.out.println("Error more accounts than allowed");
        
    }
    
    public void removeAccount(String accountNumber)
    
    {
             
      
        
        
        
        
    }
    
    /**
     * Allows user to make deposit to specified account Number
     */
   
    public void makeDeposit(String accountNumber,double amount)
    
    {
    
    }
     
      /**
     * Allows user to make withdrawl to specified account Number providing account has enough funds
     */
    
    public void makeWithdrawal(String accountNumber, double amount)
    
    {
         
        
     }
     
     public String printAccount(String accountNumber)
    
    {
         
                        
        
        
         
        
    }
    public int getNumberOfBankAccount()
    {
        return nextAccountIndex;
    }
    
    public String toString()
    
    {
        String output =" ";
        for (BankAccount ba : accounts)
        output = output + accounts + "\n";
        return output;
    }

public int findAccount(String accountNumber)
    
        
  {  
   
        int index = 0;
        for ( BankAccount ba: accounts)
       {
            if( ba.getAccountNumber().equals(accountNumber))        
            return index;             
            index++;
             
         
              }
         
            return index -1;
    }
   
       

    
    
    private void squash(int index)
    {
        if ( index>= 0)
        
        {
            for ( int i = index ; i < nextAccountIndex - 1 ; i++)
            accounts[i] = accounts [i+1];
            accounts [ nextAccountIndex - 1] = null;
            nextAccountIndex--;
        }
    }
            
}



This post has been edited by sammy123: 08 December 2010 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#6 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: search array and find index

Posted 08 December 2010 - 11:53 AM

Did you add any BankAccount objects to the accounts array before you called your findAccount() method?
Was This Post Helpful? 0
  • +
  • -

#7 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: search array and find index

Posted 08 December 2010 - 12:21 PM

Yep I sure did
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,395
  • Joined: 27-December 08

Re: search array and find index

Posted 08 December 2010 - 12:24 PM

Post your error too.
Was This Post Helpful? 0
  • +
  • -

#9 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: search array and find index

Posted 08 December 2010 - 12:45 PM

View Postmacosxnerd101, on 08 December 2010 - 11:24 AM, said:

Post your error too.


It lets me add Bankaccounts using the the array yet when I search for the account number it comes back with "expection null"
Was This Post Helpful? -1
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,395
  • Joined: 27-December 08

Re: search array and find index

Posted 08 December 2010 - 12:58 PM

Post your exact error message from the compiler. If we don't have it, we can't help you. NullPointerExceptions generally are traced through multiple methods.
Was This Post Helpful? 0
  • +
  • -

#11 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: search array and find index

Posted 08 December 2010 - 01:01 PM

I will post the exact message when I get home soon thanks
Was This Post Helpful? 0
  • +
  • -

#12 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: search array and find index

Posted 08 December 2010 - 01:28 PM

error message: java.lang.nullpointerexception: null
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,395
  • Joined: 27-December 08

Re: search array and find index

Posted 08 December 2010 - 01:30 PM

Post the stack trace. The stack trace will show a bunch of lines, each with a class, method and line number.
Was This Post Helpful? 0
  • +
  • -

#14 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: search array and find index

Posted 08 December 2010 - 01:44 PM

I have no idea about the stack trace

is this it:

java.lang.NullPointerException
at BankAccountArray.findAccount(BankAccountArray.java:141)
java.lang.NullPointerException
at BankAccountArray.findAccount(BankAccountArray.java:141)
java.lang.NullPointerException
at BankAccountArray.findAccount(BankAccountArray.java:141)
at BankAccountArray.removeAccount(BankAccountArray.java:42)
Was This Post Helpful? 0
  • +
  • -

#15 SarumanTheWhite  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 352
  • Joined: 04-November 08

Re: search array and find index

Posted 08 December 2010 - 02:10 PM

If you're going to use that object in a foreach loop it needs to implement the generic interface Iterable. Here's an example.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2