先贴代码
else if (ae.getSource() == login) {
try {
FileInputStream fis = new FileInputStream("login.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader in = new BufferedReader(isr);
String str = null;
while (in.readLine() != null) {
str = in.readLine();
String username = str.split(" ")[0];
String password = str.split(" ")[1];
if (username.equals(tfname.getText())
&& password.equals(tfpassword.getText())) {
LauchWin lw = new LauchWin();
lw.lauchWin();
this.setVisible(false);
break;
} else {
System.out.print("error");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} }
用户名和密码是写在login.txt文件里面的,用户名和密码在同一行,用空格隔开,一共存了3组,现在写这段代码只能验证到第二组用户名和密码,输入其他两组都是打印error,我不知道是哪里出了问题,希望有人指出

解决方案 »

  1.   

    readLine
    public String readLine()
                    throws IOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。 返回:
    包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 
    抛出: 
    IOException - 如果发生 I/O 错误
    while ((str = in.readLine()) != null) 
    str = in.readLine();  //不要,读了两次
      

  2.   

    你 login.txt 文件里的 用户名密码 是怎么存放的 贴出来
      

  3.   

    username1 password1
    username2 password2
    username3 password3
    是这样的格式存在txt文本里面的
      

  4.   

    楼上的说了  while (in.readLine() != null) {
                        str = in.readLine();

    读了两次改成这样
      while ((str=in.readLine()) != null) {
                   }
      

  5.   

    我一开始就是这样写的啊,改回来就只能验证第一组用户名和密码,其他两组输入正确也是打印错误
    else if (ae.getSource() == login) {
    try {
    FileInputStream fis = new FileInputStream("login.txt");
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader in = new BufferedReader(isr);
    String str = null;
    while ((str = in.readLine()) != null) {
    String username = str.split(" ")[0];
    String password = str.split(" ")[1];
    if (username.equals(tfname.getText())
    && password.equals(tfpassword.getText())) {
    LauchWin lw = new LauchWin();
    lw.lauchWin();
    this.setVisible(false);
    break;
    } else {
    System.out.print("error");
    break;
    }
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  6.   

    不知lz流程是什么?if (username.equals(tfname.getText())
                                && password.equals(tfpassword.getText())) {
                            LauchWin lw = new LauchWin();
                            lw.lauchWin();
                            this.setVisible(false);
                            break;
                        } else {
                            System.out.print("error");
                            break;
                        }break;跳出循环,不论是否找到相同的都跳出。
      

  7.   

    就是验证用户名和密码啊,如果用户名存在且和密码匹配则进入另外一个界面,继续验证就不需要了当然要break啊。如果没有找到用户名或者不匹配就提示错误循环也应该break啊,之前我测试的时候没有加break他就一直打印验证结果
      

  8.   

    自己解决了……
    try {
    FileInputStream fis = new FileInputStream("login.txt");
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader in = new BufferedReader(isr);
    String str = null;
    str = in.readLine();
    while (str != null) {
    String username = str.split(" ")[0];
    String password = str.split(" ")[1];
    if (!username.equals(tfname.getText())
    && !password.equals(tfpassword.getText())) {
    // System.out.print("cannot match");
    str = in.readLine();
    } else if (username.equals(tfname.getText())
    && password.equals(tfpassword.getText())) {
    in.close();
    LauchWin lw = new LauchWin();
    lw.lauchWin();
    this.setVisible(false);
    break;
    }
    }