因为If语句无法登录

时间:2015-12-08 17:22:29

标签: java swing if-statement

我想登录系统。当我使用ID和帐号创建我的客户时,我正在尝试输入它们并希望如果ID和帐户已连接 - 对话框将会打开。

当我创建客户ID时,在我的构造函数中,我将其转换为String:

Customer(int id, String pps, String name, String surname, String birthday, String password){

        this.id = nextId.incrementAndGet();
        Integer.toString(this.id);  

当我在另一个类中创建帐户时,但对于客户,我在构造函数中做了相同的操作(我将帐号转换为String):

public CustomerAccount(int accountNumber, double balance){

        Random n = new Random();
        this.accountNumber = 100000 + n.nextInt(900000);
        Integer.toString(this.accountNumber);

在我的驱动程序类中,我有按钮,然后,当我按下按钮时,对话框应该打开。

depositW.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                     JFrame frame = new JFrame(" ");
                     boolean found1 = false;
                     boolean found = false;


                     String a = JOptionPane.showInputDialog("Please enter the ID number: ");         
                     for(Customer aCustomer: customers){
                         System.out.println("ID: " + a + " = " + aCustomer.getId());
                         if(a.equals(aCustomer.getId())){
                             BankInterface.setCurrentCustomer(aCustomer);
                             found1 = true;
                         }else{
                  JOptionPane.showMessageDialog(frame, "There is no customer with this ID number", "Please try again", JOptionPane.WARNING_MESSAGE);
                       }

                if(found1){      
                     String b = JOptionPane.showInputDialog("Please enter the account number: ");   
                               for(CustomerAccount cu: BankInterface.getCurrentCustomer().getAccounts()){ 
                                   System.out.println("Account: " + a + " = " + cu.getAccountNumber());
                                   if(b.equals(cu.getAccountNumber())){

                                      BankInterface.setCurrentAccount(cu);
                                      found = true;
                                   }
                                }
                }

                if(found && found1){
                     new WithdrawDepositDialog(BankInterface.this, customers);
                }else{
                    JOptionPane.showMessageDialog(frame, "The username or password is not correct", "Please try again", JOptionPane.WARNING_MESSAGE);
                }
                  }
                    }
            }); 

当我测试它时,在我的控制台窗口中,我有以下内容: 因此它在运行时找不到id。

ID: 1 = 1
ID: 1 = 2

我现在肯定,错误是什么。我猜我的if语句中有些问题。这个问题的解决方案是什么?

更新代码:

String a = JOptionPane.showInputDialog("Please enter the ID number: ");
for (Customer aCustomer: customers) {
    System.out.println("ID: " + a + " = " + aCustomer.getId());
    if (a.equals(aCustomer.getId())) {
        BankInterface.setCurrentCustomer(aCustomer);
        found1 = true;
        break;
    }
}
if (!found1) {
    JOptionPane.showMessageDialog(frame, "There is no customer with this ID number", "Please try again", JOptionPane.WARNING_MESSAGE);
}

2 个答案:

答案 0 :(得分:2)

代码工作正常。

您对所有客户的循环:

- (AVAudioSessionPortDescription*)bluetoothAudioDevice
{
    NSArray* bluetoothRoutes = @[AVAudioSessionPortBluetoothA2DP, AVAudioSessionPortBluetoothLE, AVAudioSessionPortBluetoothHFP];
    return [self audioDeviceFromTypes:bluetoothRoutes];
}

- (AVAudioSessionPortDescription*)normalAudioDevice
{
    NSArray* bluetoothRoutes = @[AVAudioSessionPortBuiltInMic];
    return [self audioDeviceFromTypes:bluetoothRoutes];
}


- (AVAudioSessionPortDescription*)audioDeviceFromTypes:(NSArray*)types
{
    NSArray* routes = [[AVAudioSession sharedInstance] availableInputs];
    for (AVAudioSessionPortDescription* route in routes)
    {
        if ([types containsObject:route.portType])
        {
            return route;
        }
    }
    return nil;
}

不会在此处终止:

for(Customer aCustomer: customers){

因为found1 = true不是循环条件。你在这里缺少的是这一行之后的简单if(a.equals(aCustomer.getId())){ BankInterface.setCurrentCustomer(aCustomer); found1 = true; }

答案 1 :(得分:0)

您的客户构造函数看起来非常可疑:

Customer(int id, String pps, String name, String surname, String birthday, String password){
    this.id = nextId.incrementAndGet();
    Integer.toString(this.id);

Integer.toString(this.id);之后,this.id仍然是一个整数!

因此,a.equals(aCustomer.getId())永远不会返回true,因为您要将字符串a与整数aCustomer.getId()进行比较