2011-01-27 8 views
1

다음과 같이 구분 된 이름과 비밀번호가있는 텍스트 파일이 있습니다.텍스트 파일을 읽을 때 문제가 발생했습니다.

사용자가 올바른 사용자 이름과 암호를 제공하면 로그인 페이지에서 환영 페이지로 이동합니다. 그러나 나는 이것을 제대로 얻지 못하고있다. 내가 얻을 수있는 출력은

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

귀하의 print 문이 잘못에로 시도()

의 끝에서 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(); 

그러나이 루프가 필요하지 않습니다.

0

조건이 만족되면 휴식을 취할 수 있습니다. 반복하지 마십시오. 여기에 브레이크를 올려 놓으면 예상 결과가 나옵니다.

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

사실 이것은 'for' 루프를 깨고 실제로'while '루프가 깨지는 것이 아니라는 점에 유의하십시오. 내 대답을 보라. – aioobe

관련 문제