2015-02-24 286 views
1

我有一個for循環,用於比較用戶登錄詳細信息以啓動應用程序的下一個屏幕。如何在for循環中只執行一次else塊

如果用戶輸入的字段與從數據庫返回的ArrayList中的數據成功匹配,程序將啓動下一個屏幕 - 如果它們不匹配,則使用JOptionPane將錯誤消息輸出給用戶。

我的問題是錯誤消息是爲for循環的每次迭代輸出的,但我希望消息只顯示一次。

//if name or password are NOT left blank proceed with the check otherwise output error message in the text area 
    if (!name.equals("") && !password.equals("")) { 
     for (int i = 0; i < passwords.size(); i++) { 
      if (name.equals(userNames.get(i)) && (password.equals(passwords.get(i)))) { 
       myHomeGUI.setVisible(true); 
       break; 
      } else { 
       JOptionPane.showMessageDialog(null,"Sorry, no user recognized with those credentials\nPlease try again"); 
      } 
     }//end for loop 
    } else { 
     JOptionPane.showMessageDialog(null,"Sorry, no fields can be left blank"); 
    }//end 
+2

因此,在消息後添加一個break語句; – OldProgrammer 2015-02-24 12:32:13

+0

試過,如果我有一個休息聲明後消息在其它它不會工作。例如,如果我的arraylist for userNames的第三個位置是david,並且用戶輸入了不在數組列表中的john,它將跳出循環,並且永遠不會達到大衛,因此永遠不會評估爲True – RoRo88 2015-02-24 12:34:43

+0

我會認爲關於更改userNames /密碼的事情。爲什麼你有兩個列表? (例如)帶有用戶名/密碼的(Hash)Map 會更好嗎?那麼你不需要經過一個循環。只是比較密碼與map.get(用戶名) – griFlo 2015-02-24 12:35:45

回答

5

這是發生,因爲你把你的loopelse condtion這在每Iteration

執行試試這個:

boolean isValid=false; 
for (int i = 0; i < passwords.size(); i++) { 
      if (name.equals(userNames.get(i)) && (password.equals(passwords.get(i)))) { 
       myHomeGUI.setVisible(true); 
       isValid=true; 
       break; 
      } 
}//end for loop 
if(!isValid) { 
JOptionPane.showMessageDialog(null,"Sorry, no user recognized with those credentials\nPlease try again"); 
} 

更新

由於@Joelblade建議,你也可以將此身份驗證邏輯轉換爲sepera TE法

public boolean isAuthenticationPassed(String userName,String password){ 
       return true; // When Login Successfull 
       or 
       return false; // When Login unsuccessfull 
    } 

然後檢查你的LoginController

if(isAuthenticationPassed){ 
    // Do whatever you want to do 
} 
else{ 
//Return to Login Page with error/or show Dialog Box 
} 
+0

這幾乎是我會使用的結構。你甚至可以將整個for循環移動到一個方法中(boolean authenticateUser(name,password)),並執行:if(!authenticateUser(name,password)) – Joeblade 2015-02-24 12:42:05

+0

@Joeblade,我是模塊化方法的大力支持者,所以沒有提供太多的建議。在我的回答中加入這個建議:) – 2015-02-24 12:45:07

0

正如評論上面說:你也可以使用一個地圖,而不是兩個列表

Map<String,String> map = new HashMap<String,String>(); 
    map.put("john", "1234"); 
    map.put("test", "asdf"); 

    String name = "test"; 
    String password = "asdf"; 


    //if name or password are NOT left blank proceed with the check otherwise output error message in the text area 
    if (!name.equals("") && !password.equals("")) { 

      if(map.get(name)!=null && map.get(name).equals(password)) 
       myHomeGUI.setVisible(true); 
       break; 
      } else { 
       JOptionPane.showMessageDialog(null,"Sorry, no user recognized with those credentials\nPlease try again"); 
      }   
    } else { 
     JOptionPane.showMessageDialog(null,"Sorry, no fields can be left blank"); 

    }//end 
+0

在源代碼中,不應該暴露像這樣的密碼(或其他機密/敏感信息)。這是不好的做法 – 2015-02-24 12:47:57

+0

你當然是絕對正確的! RoRo88之前沒有使用Map,所以我只是想讓他展示如何插入數據。 – griFlo 2015-02-24 12:53:19

0

如果你重構你的代碼了到更多的邏輯部分,它將更容易編碼和理解,例如:

if (name.equals("") || password.equals("")) { 
    JOptionPane.showMessageDialog(null,"Sorry, no fields can be left blank"); 
} else if(doesUserExist(name, password)) { 
    myHomeGUI.setVisible(true); 
} else { 
    JOptionPane.showMessageDialog(null,"Sorry, no user recognized with those credentials\nPlease try again"); 
} 

// ... new method 

private boolean doesUserExist(String name, String password) { 
    for (int i = 0; i < passwords.size(); i++) { 
     if (name.equals(userNames.get(i)) && (password.equals(passwords.get(i)))) { 
      return true; 
     } 
    } 
    return false; 
}