验证用户名和密码的问题

kevinpan45 2010-11-02 01:09:54
先贴代码
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,我不知道是哪里出了问题,希望有人指出
...全文
94 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevinpan45 2010-11-02
  • 打赏
  • 举报
回复
自己解决了……
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;
}
}
kevinpan45 2010-11-02
  • 打赏
  • 举报
回复
就是验证用户名和密码啊,如果用户名存在且和密码匹配则进入另外一个界面,继续验证就不需要了当然要break啊。如果没有找到用户名或者不匹配就提示错误循环也应该break啊,之前我测试的时候没有加break他就一直打印验证结果
学习Java中 2010-11-02
  • 打赏
  • 举报
回复
不知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;跳出循环,不论是否找到相同的都跳出。
kevinpan45 2010-11-02
  • 打赏
  • 举报
回复
貌似是循环出了问题
kevinpan45 2010-11-02
  • 打赏
  • 举报
回复
我一开始就是这样写的啊,改回来就只能验证第一组用户名和密码,其他两组输入正确也是打印错误
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();
}
不善^ 2010-11-02
  • 打赏
  • 举报
回复
楼上的说了

while (in.readLine() != null) {
str = in.readLine();

读了两次

改成这样
while ((str=in.readLine()) != null) {

kevinpan45 2010-11-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 java_cxrs 的回复:]
你 login.txt 文件里的 用户名密码 是怎么存放的 贴出来
[/Quote]
username1 password1
username2 password2
username3 password3
是这样的格式存在txt文本里面的
yyy521fyy 2010-11-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baiyu123 的回复:]

while ((str = in.readLine()) != null) //读第一次
str = in.readLine(); //又读了一次,所以str被赋予第二行的值。

[/Quote]
不善^ 2010-11-02
  • 打赏
  • 举报
回复
你 login.txt 文件里的 用户名密码 是怎么存放的 贴出来
学习Java中 2010-11-02
  • 打赏
  • 举报
回复
readLine
public String readLine()
throws IOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。

返回:
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
抛出:
IOException - 如果发生 I/O 错误


while ((str = in.readLine()) != null)
str = in.readLine(); //不要,读了两次
mengyalizuopeng 2010-11-02
  • 打赏
  • 举报
回复
你是换行他也当成空格了吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧