2011-01-27 108 views
1

我有一个文本文件,我的名字和密码由:分隔。阅读文本文件的问题

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){ 

     } 
    } 

} 

请帮助我..

回答

0

我怀疑你想停止寻找的用户名,你已经找到了一个后...要做到这一点/密码匹配你在一场比赛打破循环。要做到这一点,你做到以下几点:

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]); 

它使用Scanner我可能会解决。事情是这样的:

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; 
    } 
} 
0

你的打印语句是错误在try()

结束复位nameUser的价值和密码。这就是为什么你不能正确调试。

您打印的内容与您用于名称和密码的内容不符。解决这个问题并尝试再次打印出来。

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(); 

但是,您不需要此循环。你可以使用:

 String nameUser=splitVals[0]; 
     String passWord=splitVals[1]; 
0

放休息如果satisfied.Don't让您的病情继续loop.If你把here.you得到了你的预期输出的BRAK。

if((name.equals(nameUser))&&(pwd.equals(passWord))){ 
       System.out.println("welcome"+name); 
        break; 
       } 
+0

请注意,这只会打破'for'循环,而不是实际寻找匹配的while循环。 (看我的答案。) – aioobe