2011-01-27 106 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