Java 读取文本文件时出现问题
我有一个文本文件,其中我的名字和密码之间用:Java 读取文本文件时出现问题,java,string,file,Java,String,File,我有一个文本文件,其中我的名字和密码之间用: user1:pwd1 user2:pwd2 在登录页面中,如果用户提供正确的用户名和密码,将引导您进入欢迎页面。但我没有正确地理解这一点。我得到的输出是 user1 pwd1 inside try user1 pwd1 true welcome user1 user2 pwd2 false not equal 我的代码如下 import java.io.BufferedReader; import java.io.DataInputStream;
user1:pwd1
user2:pwd2
在登录页面中,如果用户提供正确的用户名和密码,将引导您进入欢迎页面。但我没有正确地理解这一点。我得到的输出是
user1
pwd1
inside try
user1
pwd1
true
welcome user1
user2
pwd2
false
not equal
我的代码如下
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.regex.*;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
public class TextFile {
/**
* @param args
*/
public void getNamePwd(String name, String pwd) {
// TODO Auto-generated method stub
System.out.println(name);
System.out.println(pwd);
String[] splitVals=null;
try{
System.out.println("inside try");
String strLine;
BufferedReader br = new BufferedReader(new FileReader("D:\\test\\text.txt"));
while((strLine=br.readLine())!=null){
splitVals=strLine.split(":");
for(int i=0;i<splitVals.length;i=i+2){
System.out.println(splitVals[i].toString());
System.out.println(splitVals[i].toString());
String nameUser=splitVals[i].toString();
String passWord=splitVals[i+1].toString();
System.out.println(name.equals(nameUser));
if((name.equals(nameUser))&&(pwd.equals(passWord))){
System.out.println("welcome"+name);
}
else{
System.out.println("not equal");
}
}
}
}catch(Exception e){
}
}
}
导入java.io.BufferedReader;
导入java.io.DataInputStream;
导入java.io.FileInputStream;
导入java.io.FileReader;
导入java.io.InputStreamReader;
导入java.util.regex.*;
导入com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
公共类文本文件{
/**
*@param args
*/
public void getNamePwd(字符串名称,字符串pwd){
//TODO自动生成的方法存根
System.out.println(名称);
系统输出打印LN(pwd);
字符串[]splitVals=null;
试一试{
System.out.println(“内部尝试”);
弦斯特林;
BufferedReader br=新的BufferedReader(新文件读取器(“D:\\test\\text.txt”);
而((strLine=br.readLine())!=null){
splitVals=strLine.split(“:”);
对于(int i=0;i我怀疑您希望在找到用户名/密码匹配项后停止查找用户名/密码匹配项…要执行此操作,必须在匹配项时中断循环。要执行此操作,请执行以下操作:
readLoop:
while((strLine=br.readLine())!=null){
// ...
String[] splitVals = strLine.split(":");
if((name.equals(nameUser))&&(pwd.equals(passWord))){
System.out.println("welcome"+name);
break readLoop;
}
// ...
}
此外,我不知道你为什么需要这个循环:
for(int i=0;i<splitVals.length;i=i+2)
我可能会用扫描器来解决这个问题。类似这样:
System.out.printf("Username: %s, Password: %s%n", splitVals[0], splitVals[1]);
import java.io.*;
import java.util.Scanner;
public class TextFile {
public static void main(String[] args) throws FileNotFoundException {
if (userPassOk("hello", "world"))
System.out.println("Welcome");
else
System.out.println("Get out!");
}
private static boolean userPassOk(String user, String pass)
throws FileNotFoundException {
Scanner s = new Scanner(new File("test.txt"));
while (s.hasNextLine()) {
String[] userPass = s.nextLine().split(":");
if (userPass[0].equals(user) && userPass[1].equals(pass))
return true;
}
return false;
}
}
尝试在Try()结束时重置nameUser和passWord的值
如
nameUser=”“;
passWord=”“;
您的打印语句错误。这就是您无法正确调试此语句的原因
您正在打印的内容与您用于名称和密码的内容不匹配。请修复此问题,然后再次尝试打印
for(int i=0;i<splitVals.length;i=i+2){
System.out.println(splitVals[i].toString());
System.out.println(splitVals[i].toString());
String nameUser=splitVals[i].toString();
String passWord=splitVals[i+1].toString();
如果你的条件满足,就放断。不允许继续循环。如果你把刹车放在这里,你就得到了预期的输出
if((name.equals(nameUser))&&(pwd.equals(passWord))){
System.out.println("welcome"+name);
break;
}
请注意,这只会中断for
循环,而不是实际查找匹配项的while
循环(请参阅我的答案)
if((name.equals(nameUser))&&(pwd.equals(passWord))){
System.out.println("welcome"+name);
break;
}