2014-04-18 3 views
0

나는 하나의 .txt 파일에있는 모든 사용자 이름과 다른 하나의 모든 암호를 가진 좋은 로그인 프로그램을 만들었습니다. 그래서 사용자가 사용자 이름을 입력하고 첫 번째 파일의 예를 들어 5 행에있는 이름 중 하나에 해당하면 프로그램이 두 번째 파일의 다섯 번째 줄에서 암호를 읽고 주어진 암호와 일치하는지 확인합니다 사용자가 특정 파일을 읽는 방법이나 줄을 보는 방법을 모르겠습니다.Java : 파일의 특정 줄에서 정보 가져 오기

여기에있는 코드는 여기에 있습니다. 당신이 유일하게 큰 흐름을 볼 수

package databarry_; 


import java.util.*; 
import java.io.*; 
import java.lang.*; 
import javax.swing.JOptionPane; 


public class interfacelogin { 
public static void main (String[] args) { 
    boolean check1=false, check2=false, check3=false; 
    int trys = 3; 
    while (check3 == false){ 

     int id1 = 0; 
     int id2 = 0; 

     String username = null; 
     String password = null; 

     Scanner fileout = null; 
     Scanner fileout2 = null; 


     try{ 
      fileout = new Scanner(new File("username.txt")); 
     } 
     catch(Exception e){ 
      JOptionPane.showMessageDialog(null, "Fatal Error, please Reboot or reinstal program", "Boot", JOptionPane.PLAIN_MESSAGE); 
     } 
     String Username = JOptionPane.showInputDialog("Please enter your username"); 
     while(fileout.hasNext()){ 
      username = fileout.next(); 
      if(Username.equals(username)) 
       check1=true; 
     } 
     try{ 
      fileout2 = new Scanner(new File("password.txt")); 
     } 
     catch(Exception e){ 
      JOptionPane.showMessageDialog(null, "Fatal Error, please Reboot or reinstal program", "Boot", JOptionPane.PLAIN_MESSAGE); 
     } 
     String Password = JOptionPane.showInputDialog("Please enter your username"); 
     while(fileout2.hasNext()){ 
      password = fileout2.next(); 
      if(Password.equals(password) && id1 == id2) 
       check2=true; 
     } 

     if (check1 == true && check2 == true){ 
      JOptionPane.showMessageDialog(null, "succeded", "login", JOptionPane.PLAIN_MESSAGE); 
      check3 = true; 
     } else { 
      if (trys > 1){ 
      trys--; 
      JOptionPane.showMessageDialog(null, "bad login, you have " + trys + " try's remaining", "login", JOptionPane.PLAIN_MESSAGE);   
      } else { 
        JOptionPane.showMessageDialog(null, "To many bad logins, program is closing", "login", JOptionPane.PLAIN_MESSAGE); 
        check3 = true; 
      } 
     } 
    } 
} 

}

는 같은 줄에 있지 아칸소 비밀번호와 사용자 이름을 입력하면 모두 같은 (파일에서 (그래서 서로 연결되지 않음)이다 온라인 5) 사용자는 물마루를 얻습니다. 그렇게 할

답변

0

가장 좋은 방법은 두 파일을 읽고 입력 한 로그인 비밀번호 쌍 일치하는 경우, 그럼 당신은 쉽게 확인할 수있는

HashMap<String, String> 

에 로그인/암호 쌍을 저장하는 것입니다.

Map<String, String> map = new HashMap<>(); 

while(fileout.hasNext() && fileout2.hasNext()){ 
    username = fileout.next(); 
    password = fileout2.next(); 
    map.put(username, password); 
} 

... 

if (password.equals(map.get(username)) { 
    ... 
} 
+0

감사하지만 사용자 이름을 사용하고 비밀번호와 비교하는 이유는 다음과 같습니다. if (password.equals (map.get (username))) { – codermaster

+0

참조. 지도에는 키 (사용자 이름)와 값 (비밀번호)이 있으며 각 KEY에는 VALUE가 할당됩니다. .get 작업을 수행하면 KEY를 사용하여 VALUE를 찾습니다. 예 : map : (denis-> 123, codemaster-> 000). 그런 다음 map.get ("denis")는 123입니다. 그러면 사용자가 입력 한 암호를 실제 암호 (파일에서 읽음)와 비교합니다. –

+0

고맙습니다.하지만 자바에 익숙하지 않기 때문에 프로그램에서 작동하지 않습니다. (c/C++ 이전 – codermaster

0

당신은 메모리에 문제가없는 경우, 또는 사용자의 양이 너무 높지 않은 경우, 나는이 프로그램이 시작될 때 한 번만 함께 두 파일을 읽고는 HashMap에 데이터를 저장하는 것이 좋습니다 키와 사용자와 값으로 암호를 사용하여 :

BufferedReader users = new BufferedReader(new FileReader("username.txt")); 
BufferedReader passwords = new BufferedReader(new FileReader("password.txt")); 
HashMap logins = new HashMap<String, String>();   
String user,pass; 
while ((user=users.readLine())!=null) { 
    pass=passwords.readLine(); 
    logins.put(user, pass); 
} 

하고하는 것은 사용자 이름/암호 확인 :

String username = null; //the username specified by the user 
String password = null; //the password specified by the user 
if (logins.get(user).compareTo(password)==0) { 
    //correct pass 
} else { 
    //wrong pass 
} 

이 당신과 생각을주고 스케치 만입니다 (이것은 특별한 고려하지 않습니다 사례, 사용자가 존재하지 않는 사용자 이름을 삽입하는 등 ...)