簡體   English   中英

滿足條件時不顯示 JOptionPane

[英]Doesn't show the JOptionPane when a condition is met

我試圖制作一個登錄 Gui,它在文本文件中搜索密碼和用戶名,如果找不到與輸入匹配的任何內容,則它們會顯示JOptionPane但由於某種原因它從未顯示過。 這是代碼:

int determine=0;
try{
    String p1;
    String p2;
    String password =PasswordEntered.getText();
    String username =UsernameEntered.getText();
    FileReader reader=new FileReader("LoginPages.txt");
    FileReader read=new FileReader("LoginPages.txt");
    BufferedReader cr=new BufferedReader(read);
    BufferedReader br=new BufferedReader(reader);
   
    ArrayList<String> numbs = new ArrayList<String>();
    while (cr.readLine()!=null){
        numbs.add(br.readLine());
    }
    for (int i=0;i<=numbs.size();i=i+2){
        p1=numbs.get(i);
        p2=numbs.get(i+1);
        if(password.equals(p2)&&(username.equals(p1))) {
            determine=i;
            MainGUI x=new MainGUI();
            x.setVisible(true);
            dispose();
        } 
    }
    if (determine==numbs.size()) {
        JOptionPane.showMessageDialog(this, "try again");
    }
} catch(IOException ex){
    JOptionPane.showMessageDialog(this, "File Not Found");
}

它應該彈出說“再試一次”,但它什么也沒做。 請幫助或解釋任何人。

該文件已找到,否則將顯示 JOptionPane。 您可以通過重命名文件並再次運行代碼來測試它。

之后,顯示 JOptionPane 的條件將是determine==numbs.size() ,但determine被初始化為零,並且僅在成功登錄到找到它的索引時設置。 即使找到最后一個條目(用戶/密碼對),它也會與 i=size-2 匹配,因此條件永遠不會適用。

將您的代碼更改為類似

boolean found = false;
for (int i=0;i<=numbs.size();i=i+2){
    p1=numbs.get(i);
    p2=numbs.get(i+1);
    if(password.equals(p2)&&(username.equals(p1))) {
        found = true;
        break;
    } 
}
if (found) {
    MainGUI x=new MainGUI();
    x.setVisible(true);
    dispose();
} else {
    JOptionPane.showMessageDialog(this, "try again");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM