[英]Doesn't show the JOptionPane when a condition is met
Im trying to make a Login Gui where it searches a text file for the password and username and if it doesn't find any that match the input then they display a JOptionPane
but for some reason it never shows it.我试图制作一个登录 Gui,它在文本文件中搜索密码和用户名,如果找不到与输入匹配的任何内容,则它们会显示JOptionPane
但由于某种原因它从未显示过。 This is the code:这是代码:
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");
}
It was supposed to pop up saying "try again" but instead it doesn't do anything.它应该弹出说“再试一次”,但它什么也没做。 Please help or explain anyone.请帮助或解释任何人。
The file is found, otherwise a JOptionPane would have shown up.该文件已找到,否则将显示 JOptionPane。 You can test that by renaming the file and running the code again.您可以通过重命名文件并再次运行代码来测试它。
After that, the condition to display a JOptionPane would be determine==numbs.size()
, but determine
is initialized to zero and only set on successful login to the index at which it was found.之后,显示 JOptionPane 的条件将是determine==numbs.size()
,但determine
被初始化为零,并且仅在成功登录到找到它的索引时设置。 Even if the last entry (pair of user/password) were found, it would match with i=size-2, so the condition never applies.即使找到最后一个条目(用户/密码对),它也会与 i=size-2 匹配,因此条件永远不会适用。
Change your code into something like将您的代码更改为类似
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.