对2维数组字符串进行排序

时间:2015-10-24 06:39:05

标签: java arrays sorting nullpointerexception

所以对于我的程序,我正在尝试对填充了客户的银行帐户信息的二维数组进行排序。数组填充很好,当我输出内容时,一切都正确填写。但是当我尝试使用比较器对它进行排序时,它说我有一个零点异常。我似乎无法找出为什么会收到此错误。

(排序方法在底部)

    public class Customer implements Serializable
{
  public boolean istrue;
  private String    name;
  private String    id; // 3 digits string
  private String    pin;    // 4 digits string
  private ArrayList<Account> acct;
  private String [][] array;
  private double total_bal;  // for all accounts
  private int row;



  public Customer()  //constructor
  { 
      acct = new ArrayList<>(100);
      row = 0;
      array = new String[80][5];
      istrue = true;
      name = "NoName";
      id = "000";
      pin = "0000";
      total_bal = 0;

  }

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getPin() {
    return pin;
}

public void setPin(String pin) {
    this.pin = pin;
    if (pin.length() != 4) // check to see if PIN is less or greater than 4 
    {
        System.out.println("That was not 4 digits.");
    }
    else istrue = false;

}

public void fill_acct(Account acct_obj)
{
    acct.add(acct_obj);
}

public ArrayList<Account> getAcct() {
    return acct;
}
public boolean acct_check(String account, boolean isfound)
{
    for (Account act : acct)
    {
        if (account.equals(act.getNumber()))
        {
            isfound = true;
        }

    }
    return isfound;
}

public void acct_deposit(String account, double deposit)
{
    for (Account act : acct )
    {
        if (account.equals(act.getNumber()))
        {
            act.setBalance(deposit);
            System.out.print("Account " + act.getNumber() + " balance: ");
            System.out.println(act.getBalance());
        }
    }
}
public void acct_info(String account)
{

        for (Account act : acct )
        {
            if (account.equals(act.getNumber()))
            {   
                if(act.getType().equals("checking"))
                {
                System.out.print("Checkings account number: ");
                System.out.println(act.getNumber());
                System.out.print("Checkings account balance: ");
                System.out.println(act.getBalance());
                }
                if(act.getType().equals("saving"))
                {
                System.out.print("Savings account number: ");
                System.out.println(act.getNumber());
                System.out.print("Savings account balance: ");
                System.out.println(act.getBalance());
                }
            }
        }   




}

public void acct_withdraw(String account, double amount)
{

    for (Account act : acct)
    {
        if (account.equals(act.getNumber()))
        {
            act.setBalance(-(amount));
            System.out.print("Account " + act.getNumber() + " balance: ");
            System.out.println(act.getBalance());
        }
    }
}

public void acct_transfer(String w_account, String d_account, double amount)
{
    acct_withdraw(w_account, amount);
    acct_deposit(d_account, amount);
}

public void close_account(String account)
{
    for (Account act : acct)
    {
        if (account.equals(act.getNumber()))
        {
            act.setZero();
            act.setActive(false);
            System.out.print("Account " + act.getNumber() + " balance: ");
            System.out.println(act.getBalance());
        }
    }


}
public void interest_check(int counter, int interest_rate)
{
    if (counter % 5 == 0)
    {
        System.out.println("5 Transactions: Compounding interest! ");
        for(Account act: acct)
        {
            if (act.getType().equals("saving"))
            {
                act.compound_interest(interest_rate);
            }
        }
    }

}

public void acct_num()
{
    for (Account act : acct)
    {
        System.out.println(name + "          " + act.getNumber() + "        " + pin + "          "
        + act.getBalance() + "        " + id);
    }
}

public void fill_array()
    {

    for (Account act : acct)
    {

        for(int col = 0; col < 5; col++)
        {
            if (col == 0)
            {
                array[row][col] = name;
            }
            if (col == 1)
            {
                array[row][col] = act.getNumber();
            }
            if (col == 2)
            {
                array[row][col] = pin;
            }
            if (col == 3)
            {
                array[row][col] = String.valueOf(act.getBalance());
            }
            if (col == 4)
            {
                array[row][col] = id;
            }   
        }   
        row++;
    }

    }

public void sort_array()
{   for (int x = 0; x < row; x++)
{
    for (int y = 0; y < 5; y++)
    {
        System.out.print(array[x][y] + "           ");
        if (y == 4)
        {
            System.out.println();
        }
    }
}
    Arrays.sort(array, new Comparator<String[]>(){

    @Override
    public int compare(final String[] first, final String[] second){
        return Double.valueOf(second[3]).compareTo(Double.valueOf(first[3])
        );
    }
});

0 个答案:

没有答案